下面列出了android.graphics.drawable.BitmapDrawable#setTileModeXY() 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Tiles the background of the for a view with viewId as a parameter.
*/
public static void tileBackground(Context ctx, int viewId, int resIdOfTile) {
try {
// Tiling the background.
Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), resIdOfTile);
BitmapDrawable bitmapDrawable = new BitmapDrawable(ctx.getResources(), bmp);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
View view = ((Activity) ctx).findViewById(viewId);
if (view == null) {
throw new NullPointerException("View to which the tile has to be applied should not be null");
} else {
setBackground(view, bitmapDrawable);
}
} catch (Exception e) {
Log.w(TAG, "#tileBackground Exception while tiling the background of the view");
}
}
public static void tileBackground(Context ctx, int layoutId, View viewToTileBg, int resIdOfTile) {
try {
// Tiling the background.
Bitmap bmp = BitmapFactory.decodeResource(ctx.getResources(), resIdOfTile);
// deprecated constructor
// BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
BitmapDrawable bitmapDrawable = new BitmapDrawable(ctx.getResources(), bmp);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
View view = viewToTileBg.findViewById(layoutId);
if (view != null) {
setBackground(view, bitmapDrawable);
}
} catch (Exception e) {
Log.e(TAG, "Exception while tiling the background of the view");
}
}
/**
* Fix the repeatable background of a bitmap drawable.
* This only support a BitmapDrawable
* @param d the BitmapDrawable to set repeatable.
*/
public static void fixRepeatableBitmapDrawable(BitmapDrawable d) {
if(!needRepeatableFix()) {
return;
}
// I don't want to mutate because it's better to share the drawable fix for all that share this constant state
//d.mutate();
//Log.d(THIS_FILE, "Exisiting tile mode : " + d.getTileModeX() + ", "+ d.getTileModeY());
d.setTileModeXY(d.getTileModeX(), d.getTileModeY());
}
/**
* Initialises the paint drawing elements.
*/
private void init() {
mTouchPath = new Path();
mErasePaint = new Paint();
mErasePaint.setAntiAlias(true);
mErasePaint.setDither(true);
mErasePaint.setColor(0xFFFF0000);
mErasePaint.setStyle(Paint.Style.STROKE);
mErasePaint.setStrokeJoin(Paint.Join.BEVEL);
mErasePaint.setStrokeCap(Paint.Cap.ROUND);
setStrokeWidth(6);
mGradientBgPaint = new Paint();
mErasePath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
Bitmap scratchBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_scratch_pattern);
mDrawable = new BitmapDrawable(getResources(), scratchBitmap);
mDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
setEraserMode();
}
/**
* Initialises the paint drawing elements.
*/
private void init() {
mTouchPath = new Path();
mErasePaint = new Paint();
mErasePaint.setAntiAlias(true);
mErasePaint.setDither(true);
mErasePaint.setColor(0xFFFF0000);
mErasePaint.setStyle(Paint.Style.STROKE);
mErasePaint.setStrokeJoin(Paint.Join.BEVEL);
mErasePaint.setStrokeCap(Paint.Cap.ROUND);
mErasePaint.setXfermode(new PorterDuffXfermode(
PorterDuff.Mode.CLEAR));
setStrokeWidth(6);
mGradientBgPaint = new Paint();
mErasePath = new Path();
mBitmapPaint = new Paint(Paint.DITHER_FLAG);
Bitmap scratchBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_scratch_pattern);
mDrawable = new BitmapDrawable(getResources(), scratchBitmap);
mDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
}
private Drawable[] createDrawables(Bitmap[] bitmaps) {
Drawable[] pieces = new Drawable[bitmaps.length];
for (int i = 0; i < bitmaps.length; i++) {
BitmapDrawable bitmapDrawable = new BitmapDrawable(context.getResources(), bitmaps[i]);
bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.CLAMP);
if (i == 0) {
pieces[i] = bitmapDrawable;
} else {
pieces[i] = new ClipDrawable(bitmapDrawable, Gravity.LEFT, ClipDrawable.HORIZONTAL);
}
}
return pieces;
}
/**
* This creates a checkerboard style background displayed before the image is shown.
*/
private void setBackgroundForImageView(ImageView imageView, Resources resources) {
Drawable drawable =
ApiCompatibilityUtils.getDrawable(resources, R.drawable.checkerboard_background);
Bitmap bitmap = Bitmap.createBitmap(drawable.getIntrinsicWidth(),
drawable.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
drawable.setBounds(0, 0, canvas.getWidth(), canvas.getHeight());
drawable.draw(canvas);
BitmapDrawable bm = new BitmapDrawable(resources, bitmap);
bm.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
imageView.setVisibility(View.VISIBLE);
imageView.setBackground(bm);
}
public BitmapDrawable getBackgroundDrawable(final Context context, final int patternID, final int color) {
BitmapDrawable out = new BackgroundBitmapDrawable(context.getResources(), mBitmaps[validatePatternID(patternID)]);
out.setAntiAlias(true);
out.setDither(true);
out.setTileModeXY(android.graphics.Shader.TileMode.REPEAT, android.graphics.Shader.TileMode.REPEAT);
out.setColorFilter(new PorterDuffColorFilter(color, android.graphics.PorterDuff.Mode.MULTIPLY));
return out;
}
/**
* 生成水印文字图片
*/
public BitmapDrawable drawTextToBitmap(Context mContext, String gText) {
try {
TextPaint mTextPaint1 = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint1.density = mContext.getResources().getDisplayMetrics().density;
oneTextPx = Utils.sp2px(mContext, textSize);
mTextPaint1.setTextSize(oneTextPx);
//计算字长
textLength = (int) mTextPaint1.measureText(gText);
int stextLength = (int) mTextPaint1.measureText(sText);
/**拿到字长之后,计算一下斜着显示文字的时候文字所占的长和高*/
int witchPx = 0;
int highPx = 0;
/**默认一段文字的长和高计算*/
if (sWitchPx == 0) {
sWitchPx = measurementWitch(stextLength);
sHigthPx = measurementHigth(stextLength);
}
/**传入的文字的长和高计算*/
witchPx = measurementWitch(textLength);
highPx = measurementHigth(textLength);
/**计算显示完整所需占的画图长宽*/
int bitmapWitch = witchPx + sWitchPx + 2 * oneTextPx + offset;
int bitmaphigth = (highPx + oneTextPx) * 2;
//设置画板的时候 增加一个字的长宽
bitmap = Bitmap.createBitmap(bitmapWitch + right,
bitmaphigth+(2*top), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
canvas.drawColor(ContextCompat.getColor(mContext, R.color.atom_ui_chat_gray_bg));
/**初始化画笔*/
TextPaint mTextPaint = new TextPaint(Paint.ANTI_ALIAS_FLAG);
mTextPaint.density = mContext.getResources().getDisplayMetrics().density;
mTextPaint.setColor(Color.parseColor("#C4C4C4"));
mTextPaint.setAlpha(90);
mTextPaint.setStyle(Paint.Style.FILL);
mTextPaint.setAntiAlias(true);
mTextPaint.setTextAlign(Paint.Align.LEFT);
mTextPaint.setFakeBoldText(false);
mTextPaint.setTextSkewX(0);
mTextPaint.setTextSize(oneTextPx);
/**
* ——————————————————————————————————>
* | 1号绘制区域 | 间 | 2号 |
* | gText | | appName |
* | ①号起点位置 | 隔 | ②起 |
* ———————————————————————————————————
* | 3号 | 间 | 4号绘制区域 |
* | appName | | gText |
* | ③起 | 隔 | ④号起 |
* |———————————————————————————————————
* V
*/
/**方式二利用画布平移和旋转绘制*/
/**先移动到①起点位置*/
canvas.translate(oneTextPx,highPx+oneTextPx+top);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(gText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到②号位置*/
canvas.rotate(rotate);
canvas.translate(witchPx+offset+oneTextPx,0);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(sText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到③号位置*/
canvas.rotate(rotate);
canvas.translate(-(witchPx+offset+oneTextPx),oneTextPx+highPx+top);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(sText,0,0,mTextPaint);
/**恢复原来的度数 再移动画布原点到④号位置*/
canvas.rotate(rotate);
canvas.translate(sWitchPx+offset+oneTextPx,0);
/**旋转一定度数 绘制文字*/
canvas.rotate(-rotate);
canvas.drawText(gText,0,0,mTextPaint);
/**保存画布*/
canvas.save(Canvas.ALL_SAVE_FLAG);
canvas.restore();
//生成平铺的bitmapDrawable
BitmapDrawable drawable = new BitmapDrawable(mContext.getResources(), bitmap);
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.setDither(true);
return drawable;
} catch (Exception e) {
}
return null;
}
public static Bitmap getTiled(final Bitmap source, final int xSize, final int ySize, final boolean tile, final String background_file) {
Bitmap tiledBitmap = null;
if (background_file != null) {
try {
tiledBitmap = BitmapLoader.bitmapFromBundleCache(background_file);
if (tiledBitmap == null) {
android.util.Log.d("NumberWall", "Regenerating image");
final Uri background_uri = Uri.parse(background_file);
FileDescriptor fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor();
final Bitmap image_bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
Bitmap rotated_bitmap = image_bitmap;
final Matrix image_matrix = new Matrix();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor(); // reset
final ExifInterface exif = new ExifInterface(fileDescriptor);
int rotation = exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
android.util.Log.d("NumberWall", "Rotation: " + rotation);
if (rotation != 0) {
image_matrix.preRotate(rotation);
rotated_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), image_matrix, true);
image_bitmap.recycle();
}
}
tiledBitmap = getBestCroppedScaled(rotated_bitmap, xSize, ySize);
BitmapLoader.saveBitmapAsBundle(background_file, Bitmap.createBitmap(tiledBitmap));
} else {
tiledBitmap = Bitmap.createBitmap(tiledBitmap); // make a copy
android.util.Log.d("NumberWall", "cache hit");
}
} catch (Exception e) {
// cannot load bitmap
android.util.Log.e("NumberWall", "Cannot load bitmap: " + e);
}
}
if (tiledBitmap == null) {
tiledBitmap = Bitmap.createBitmap(xSize, ySize, Bitmap.Config.ARGB_8888);
}
if (source != null) {
final Canvas canvas = new Canvas(tiledBitmap);
final BitmapDrawable drawable = new BitmapDrawable(xdrip.getAppContext().getResources(), source);
drawable.setBounds(0, 0, xSize, ySize);
if (tile) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.draw(canvas);
} else {
double y_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_y_param", ""), 50d) / 100d; // TODO move to method signature
int yoffset = Math.max(0, (int) ((getScreenDpi() * y_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
final double spacer_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_s_param", ""), 10d) / 100d;
int xoffset = Math.max(0, (int) ((getScreenDpi() * spacer_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
canvas.drawBitmap(source, xoffset, yoffset, new Paint());
}
source.recycle();
} // returns blank bitmap if source is null
return tiledBitmap;
}
public static Bitmap getTiled(final Bitmap source, final int xSize, final int ySize, final boolean tile, final String background_file) {
Bitmap tiledBitmap = null;
if (background_file != null) {
try {
tiledBitmap = BitmapLoader.bitmapFromBundleCache(background_file);
if (tiledBitmap == null) {
android.util.Log.d("NumberWall", "Regenerating image");
final Uri background_uri = Uri.parse(background_file);
FileDescriptor fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor();
final Bitmap image_bitmap = BitmapFactory.decodeFileDescriptor(fileDescriptor);
Bitmap rotated_bitmap = image_bitmap;
final Matrix image_matrix = new Matrix();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
fileDescriptor = xdrip.getAppContext().getContentResolver().openFileDescriptor(background_uri, "r").getFileDescriptor(); // reset
final ExifInterface exif = new ExifInterface(fileDescriptor);
int rotation = exifOrientationToDegrees(exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL));
android.util.Log.d("NumberWall", "Rotation: " + rotation);
if (rotation != 0) {
image_matrix.preRotate(rotation);
rotated_bitmap = Bitmap.createBitmap(image_bitmap, 0, 0, image_bitmap.getWidth(), image_bitmap.getHeight(), image_matrix, true);
image_bitmap.recycle();
}
}
tiledBitmap = getBestCroppedScaled(rotated_bitmap, xSize, ySize);
BitmapLoader.saveBitmapAsBundle(background_file, Bitmap.createBitmap(tiledBitmap));
} else {
tiledBitmap = Bitmap.createBitmap(tiledBitmap); // make a copy
android.util.Log.d("NumberWall", "cache hit");
}
} catch (Exception e) {
// cannot load bitmap
android.util.Log.e("NumberWall", "Cannot load bitmap: " + e);
}
}
if (tiledBitmap == null) {
tiledBitmap = Bitmap.createBitmap(xSize, ySize, Bitmap.Config.ARGB_8888);
}
if (source != null) {
final Canvas canvas = new Canvas(tiledBitmap);
final BitmapDrawable drawable = new BitmapDrawable(xdrip.getAppContext().getResources(), source);
drawable.setBounds(0, 0, xSize, ySize);
if (tile) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
drawable.draw(canvas);
} else {
double y_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_y_param", ""), 50d) / 100d; // TODO move to method signature
int yoffset = Math.max(0, (int) ((getScreenDpi() * y_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
final double spacer_ratio = JoH.tolerantParseDouble(Pref.getString("numberwall_s_param", ""), 10d) / 100d;
int xoffset = Math.max(0, (int) ((getScreenDpi() * spacer_ratio * 1.2d) - (getScreenDpi() * 0.30d)));
canvas.drawBitmap(source, xoffset, yoffset, new Paint());
}
source.recycle();
} // returns blank bitmap if source is null
return tiledBitmap;
}
private FrameLayout initializeCroutonViewGroup(Resources resources) {
FrameLayout croutonView = new FrameLayout(this.activity);
if (null != onClickListener) {
croutonView.setOnClickListener(onClickListener);
}
final int height;
if (this.style.heightDimensionResId > 0) {
height = resources.getDimensionPixelSize(this.style.heightDimensionResId);
} else {
height = this.style.heightInPixels;
}
final int width;
if (this.style.widthDimensionResId > 0) {
width = resources.getDimensionPixelSize(this.style.widthDimensionResId);
} else {
width = this.style.widthInPixels;
}
croutonView.setLayoutParams(
new FrameLayout.LayoutParams(width != 0 ? width : FrameLayout.LayoutParams.MATCH_PARENT, height));
// set background
if (this.style.backgroundColorValue != Style.NOT_SET) {
croutonView.setBackgroundColor(this.style.backgroundColorValue);
} else {
croutonView.setBackgroundColor(resources.getColor(this.style.backgroundColorResourceId));
}
// set the background drawable if set. This will override the background
// color.
if (this.style.backgroundDrawableResourceId != 0) {
Bitmap background = BitmapFactory.decodeResource(resources, this.style.backgroundDrawableResourceId);
BitmapDrawable drawable = new BitmapDrawable(resources, background);
if (this.style.isTileEnabled) {
drawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);
}
croutonView.setBackgroundDrawable(drawable);
}
return croutonView;
}