有没有办法在android中只垂直重复图像?我试过这样:
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="@drawable/asphalt_texture"
android:tileMode="repeat"
android:dither="true"/>
但它以两种方式重复,我只希望它垂直重复,任何想法都将不胜感激!
奇怪的是,似乎没有办法在 XML 中做到这一点。但是,通过代码,您可以。以下方法:
BitmapDrawable.setTileModeX(Shader.TileMode mode)
BitmapDrawable.setTileModeY(Shader.TileMode mode)
应该做你需要的。只需为您需要重复效果的任何轴(垂直轴为 y 轴)传入其中一个Shader.TileMode
枚举 ( REPEAT
, MIRROR
, )。CLAMP
所以你应该能够做这样的事情:
BitmapDrawable draw = (BitmapDrawable)getResources().getDrawable(R.drawable.draw);
draw.setTileModeY(Shader.TileMode.REPEAT);
我觉得这很简单:(此代码将在 Y 中平铺并在 x 中重复)
在您的 onWindowFoucsChanged 中,您调用:
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
super.onWindowFocusChanged(hasFocus);
Drawable d = getRepeatingBG(this, R.drawable.image_that_you_want_to_repeat);
body_view.setBackgroundDrawable(d);
}
private Drawable getRepeatingBG(Activity activity, int center)
{
DisplayMetrics dm = new DisplayMetrics();
activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled=true;
Bitmap center_bmp = BitmapFactory.decodeResource(activity.getResources(), center, options);
center_bmp.setDensity(Bitmap.DENSITY_NONE);
center_bmp=Bitmap.createScaledBitmap(center_bmp, dm.widthPixels , center_bmp.getHeight(), true);
BitmapDrawable center_drawable = new BitmapDrawable(activity.getResources(),center_bmp);
//change here setTileModeY to setTileModeX if you want to repear in X
center_drawable.setTileModeY(Shader.TileMode.REPEAT);
return center_drawable;
}