这个createRoundDrawable里面是生成一个圆形透明图?里面的算法不太懂
private RoundedBitmapDrawable createRoundDrawable(Bitmap bitmap, final int color) {
int thumbWidth = mThumbnailView.getLayoutParams().width;
int thumbHeight = mThumbnailView.getLayoutParams().height;
//setContentDescription is added for testing
mThumbnailView.setContentDescription("Has Content");
// crop a square bitmap according minimum edge.
if (bitmap == null) {
bitmap = Bitmap.createBitmap(thumbWidth, thumbHeight,
Bitmap.Config.ARGB_8888);
mThumbnailView.setContentDescription("No Content");
}
int bmpWidth = bitmap.getWidth();
int bmpHeight = bitmap.getHeight();
Bitmap squareBitmap;
if (bmpHeight > bmpWidth) {
squareBitmap = Bitmap.createBitmap(bitmap, 0,
(bmpHeight - bmpWidth) / 2, bmpWidth, bmpWidth);
} else if (bmpHeight < bmpWidth) {
squareBitmap = Bitmap.createBitmap(bitmap,
(bmpWidth - bmpHeight) / 2, 0, bmpHeight, bmpHeight);
} else {
squareBitmap = bitmap;
}
// center scale to the size of thumbnail view.
Bitmap scaledBitmap = Bitmap.createScaledBitmap(squareBitmap,
thumbWidth, thumbHeight, true);
// Calculate the bitmap radius
int borderWidthHalf = 2;
int bitmapWidth = scaledBitmap.getWidth();
int bitmapHeight = scaledBitmap.getHeight();
int bitmapRadius = Math.min(bitmapWidth, bitmapHeight) / 2;
int bitmapSquareWidth = Math.min(bitmapWidth, bitmapHeight);
int newBitmapSquareWidth = bitmapSquareWidth - borderWidthHalf * 2;
/*
* Initializing a new empty bitmap. Set the bitmap size from source
* bitmap Also add the border space to new bitmap
*/
//if (mRoundedBitmap != null) {
// mRoundedBitmap.recycle();
// mRoundedBitmap = null;
//}
mRoundedBitmap = Bitmap.createBitmap(newBitmapSquareWidth,
newBitmapSquareWidth, Bitmap.Config.ARGB_8888);
// Initialize a new Canvas to draw empty bitmap
Canvas canvas = new Canvas(mRoundedBitmap);
// Draw a solid color to canvas
canvas.drawColor(color);
// Calculation to draw bitmap at the circular bitmap center position
int centerX = newBitmapSquareWidth - bitmapWidth;
int centerY = newBitmapSquareWidth - bitmapHeight;
/*
* Now draw the bitmap to canvas. Bitmap will draw its center to
* circular bitmap center by keeping border spaces
*/
canvas.drawBitmap(scaledBitmap, centerX, centerY, null);
// Initializing a new Paint instance to draw circular border
Paint borderPaint = new Paint();
borderPaint.setStyle(Paint.Style.STROKE);
borderPaint.setStrokeWidth(borderWidthHalf * 2);
borderPaint.setColor(BORDER_COLOR);
/*
* Draw the circular border to bitmap. Draw the circle at the center of
* canvas.
*/
canvas.drawCircle(canvas.getWidth() / 2, canvas.getWidth() / 2,
newBitmapSquareWidth / 2, borderPaint);
// Create a new RoundedBitmapDrawable
RoundedBitmapDrawable roundedBitmapDrawable = RoundedBitmapDrawableFactory
.create(mApp.getActivity().getResources(), mRoundedBitmap);
// Set the corner radius of the bitmap drawable
roundedBitmapDrawable.setCornerRadius(bitmapRadius);
roundedBitmapDrawable.setAntiAlias(true);
bitmap.recycle();
squareBitmap.recycle();
scaledBitmap.recycle();
bitmap = null;
squareBitmap = null;
scaledBitmap = null;
return roundedBitmapDrawable;
}
正在回答 回答被采纳积分+1
请问构造方法:
public ThumbnailViewManager(IApp app, ViewGroup parentView) {
super(app, parentView);
mContext = app.getActivity();
}
有些构造方法是这样:
public ThumbnailViewManager(IApp app, ViewGroup parentView) {
app= **;
parentView = **;
}
请问这两种构造方法有什么区别?
那两个值加大了之后,这个入口这里变成了不是圆形而是正方形。
这个值加大过的,没用,反而就是我改thumbWidth,thumbHeight的时候窗口才有变化,现在如图中窗口太小。
请问这个生成的圆形,哪个能将这里设置的值能将圆加大呢?谢谢!
- 参与学习 人
- 提交作业 116 份
- 解答问题 1012 个
本阶段是提升项目经验的必备,除Android开发的高级控件,还有Android官方大力推荐的开发语言Kotlin,未来Android发展的方向,最后使用Kotlin来开发热门电商项目。
了解课程
恭喜解决一个难题,获得1积分~
来为老师/同学的回答评分吧
0 星