[英]change part of image color
在我的應用程序中,我想更改下面圖像中的文本顏色,當用戶單擊每個單詞時,我在數據庫中確定了每個單詞的位置,並使用canvas.drawRect
繪制了一個矩形,但我想更改ImageView中文本的確切顏色:
我在Imageview中的代碼是:
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setColor(Color.parseColor("#5DFFF700"));
paint.setStyle(Paint.Style.FILL_AND_STROKE);
if (highlight) {
float rightx = thisL.maxX/scale;
float leftx = thisL.minX/scale;
float bottomy = thisL.maxY/scale;
float topy= thisL.minY/scale;
canvas.drawRect(leftx, topy, rightx, bottomy, paint);
}
//slctd word
if (slctdWord!=null) {
paint.setStyle(Paint.Style.FILL_AND_STROKE);
float wrightx = slctdWord.max_x / scale;
float wleftx = slctdWord.min_x / scale;
float wbottomy = slctdWord.max_y / scale;
float wtopy = slctdWord.min_y / scale;
canvas.drawRect(wleftx, wtopy, wrightx, wbottomy, paint);
}
}
最后,我裁剪了我的imageview的一部分,將其更改顏色並用畫布繪制:(onDraw)
Bitmap bm=((BitmapDrawable)getDrawable()).getBitmap();
Bitmap crdb = changeBitmapColor(Bitmap.createBitmap(bm,
Math.round(leftx), Math.round(topy), Math.round(rightx-leftx),Math.round(bottomy-topy)));
canvas.drawBitmap(crdb,leftx,topy,null);
方法changeBitmapColor
public Bitmap changeBitmapColor(Bitmap sourceBitmap)
{
Bitmap resultBitmap = sourceBitmap.copy(sourceBitmap.getConfig(),true);
Paint paint = new Paint();
ColorFilter filter = new LightingColorFilter(Color.WHITE, Color.RED);
paint.setColorFilter(filter);
Canvas canvas = new Canvas(resultBitmap);
canvas.drawBitmap(resultBitmap, 0, 0, paint);
return resultBitmap;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.