三種方法實(shí)現(xiàn)Android平鋪效果
作者:佚名
這里提供了三種方法實(shí)現(xiàn)Android平鋪效果,分別是利用系統(tǒng)提供的api實(shí)現(xiàn)、使用xml來輕松實(shí)現(xiàn)和自己畫出來。
需要實(shí)現(xiàn)平鋪效果,大致有幾下幾種方法。
***種,利用系統(tǒng)提供的api實(shí)現(xiàn):
- Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.pic);
- //bitmap = Bitmap.createBitmap(200, 30, Config.ARGB_8888);
- BitmapDrawable drawable = new BitmapDrawable(bitmap);
- drawable.setTileModeXY(TileMode.REPEAT , TileMode.REPEAT );
- drawable.setDither(true);
- view.setBackgroundDrawable(drawable);
第二種,使用xml來輕松實(shí)現(xiàn),貌似是4.0以后出現(xiàn)的:
- <bitmap xmlns:Android="http://schemas.android.com/apk/res/android"
- android:src="../../@drawable/img"
- Android:tileMode="repeat" />
第三種,自己畫出來:
- public static Bitmap createRepeater(int width, Bitmap src){
- int count = (width + src.getWidth() - 1) / src.getWidth();
- Bitmap bitmap = Bitmap.createBitmap(width, src.getHeight(), Config.ARGB_8888);
- Canvas canvas = new Canvas(bitmap);
- for(int idx = 0; idx < count; ++ idx){
- canvas.drawBitmap(src, idx * src.getWidth(), 0, null);
- }
- return bitmap;
- }
責(zé)任編輯:徐川
來源:
OSChina