2013-08-29 12 views
7

res/drawable/schoolboard.pngの画像に円を描こうとしています。イメージはアクティビティの背景を塗りつぶします。次のように動作しません:既存の画像に円を描く

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard); 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.BLUE); 

    Canvas canvas = new Canvas(bitmap); 
    canvas.drawCircle(60, 50, 25, paint); 

    ImageView imageView = (ImageView)findViewById(R.drawable.schoolboard); 
    imageView.setAdjustViewBounds(true); 
    imageView.setImageBitmap(bitmap); 

すべてのヘルプが高く評価されます。ありがとう。

答えて

10

は、あなたのコードではエラーがあります:における画像IDあなたはfindViewByIdで描画可能なため参照IDを与えることができないものの 最初は ので、私はあなたがその

ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view); 

schoolboard_image_viewのようなものを意味すると考えているあなたXMLレイアウト(右idのレイアウトを確認してください)

BitmapFactory.Options myOptions = new BitmapFactory.Options(); 
    myOptions.inDither = true; 
    myOptions.inScaled = false; 
    myOptions.inPreferredConfig = Bitmap.Config.ARGB_8888;// important 
    myOptions.inPurgeable = true; 

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.schoolboard,myOptions); 
    Paint paint = new Paint(); 
    paint.setAntiAlias(true); 
    paint.setColor(Color.BLUE); 


    Bitmap workingBitmap = Bitmap.createBitmap(bitmap); 
    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true); 


    Canvas canvas = new Canvas(mutableBitmap); 
    canvas.drawCircle(60, 50, 25, paint); 

    ImageView imageView = (ImageView)findViewById(R.id.schoolboard_image_view); 
    imageView.setAdjustViewBounds(true); 
    imageView.setImageBitmap(mutableBitmap); 

のために右の画像IDを使用していることを確認してください:(ImageView)findViewById(R.id.schoolboard_image_view);

+1

お互いにありがとう! RelativeLayoutでスクールボードイメージが 'android:background =" @ drawable/schoolboard "と定義される前に、xmlファイルをchanしてイメージビューを作成しました。今それは動作します! – Kfir

5

まずBitmapFactory.decodeResource()メソッドのビットマップは不変なので、新しいビットマップを作成する必要があります。これを行うには、次のコードを使用します。

Bitmap canvasBitmap = Bitmap.createBitmap([bitmap_width], [bitmap_height], Config.ARGB_8888); 

このビットマップをキャンバスコンストラクタで使用します。次に、ビットマップをキャンバスに描画します。

Canvas canvas = new Canvas(canvasBitmap); 
canvas.drawBitmap(bitmap, 0, 0, bitmapPaint); 
canvas.drawCircle(60, 50, 25, paint); 

また、R.drawable.schoolboardは正しいビューIDではありません。

ImageView imageView =(ImageView)findViewById(R.drawable.schoolboard);