2013-02-10 56 views
-8

如何让此代码正常工作?我想要一个图像变成一个哭泣,我发现这个网站上的代码,但它并没有说如何使用它...任何人有任何想法?在ImageView上应用位图转换

Cropping circular area from bitmap in Android

public Bitmap getCroppedBitmap(Bitmap bitmap) { 
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), 
      bitmap.getHeight(), Config.ARGB_8888); 
    Canvas canvas = new Canvas(output); 

    final int color = 0xff424242; 
    final Paint paint = new Paint(); 
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight()); 

    paint.setAntiAlias(true); 
    canvas.drawARGB(0, 0, 0, 0); 
    paint.setColor(color); 
    // canvas.drawRoundRect(rectF, roundPx, roundPx, paint); 
    canvas.drawCircle(bitmap.getWidth()/2, bitmap.getHeight()/2, 
      bitmap.getWidth()/2, paint); 
    paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN)); 
    canvas.drawBitmap(bitmap, rect, rect, paint); 
    //Bitmap _bmp = Bitmap.createScaledBitmap(output, 60, 60, false); 
    //return _bmp; 
    return output; 
} 

回答

0

比方说,你必须要使用可拉伸?

image对象 - 可以是ImageView的:

Bitmap bitImg = BitmapFactory.decodeResource(getResources(), 
    R.drawable.YOUR_DRAWABLE_NAME); 
image.setImageBitmap(getCroppedBitmap(bitImg)); 
0

您将您绘制成位图,通过它,并将其设置为一个ImageView的(我想这是你想要的):

Bitmap myDrawable = BitmapFactory.decodeResource(getResources(), R.drawable.myDrawable); 

myImageView.setImageBitmap(getCroppedBitmap(myDrawable));