2011-02-07 17 views
23

私はアンドロイドプログラミングに慣れています。私はこれを理解しようとしています。Android:ImageViewにキャンバスを描く

私のレイアウトでは、TextView、ImageView、およびButtonがすべて、垂直方向のLinearLayoutにあります。

残りのレイアウト(テキストビュー/ボタン)に影響を与えずに、ImageViewで円をダイナミックに描画したいと考えています。私はキャンバスを作成しようとしていて、キャンバス内のdrawcircle関数を使って円の位置を設定しています。そして、そのキャンバスを何らかの方法で私のイメージビューに描画します。私はこれを働かせることができません、これにトリックがありますか?または私の方法は根本的に間違っていますか?レイアウト全体を再作成せずにImageViewにサークルを描画する方法は?

ありがとうございます!

答えて

0

レイアウトがxmlで、すべての要素が垂直方向に配置されている場合は、

クラスビューを拡張してクラスを作成し、そのonDrawメソッドをオーバーライドすることで、パッケージ内のクラスを作成して、必要なものを実現できます。あなたが望むように円を描きます。

その後、代わりにレイアウトでImageViewのを追加し、次の

<のLinearLayout>

< TextView> < /TextView> 

    < Button> < /Button> 

    <com.prac.MyView> </ com.prac.MyView> 

< /のLinearLayout>

layout.like XMLでこの独自のビューを追加するための次のリンクをチェック2Dグラフィックス。それは読むための素晴らしいチュートリアルです。
http://organicandroid.blogspot.com/2010/08/starting-to-play-with-graphics.html
・ホープ、このヘルプ:)あなたが欲しいものを行うにはいくつかの方法がありますが、記述方法はそのうちの一つではありませんImageViewのを使用してあります

+0

リンクを更新してください。ブログは削除されました –

0

。 1つの可能性は、ImageViewにアニメーション化されたDrawableを表示させることです。 Drawable実装でサークルを描画することに集中することができます。別の方法は、イメージを変更するたびに新しいビットマップを作成し、新しいビットマップを表示するようにImageViewを設定することです。ただし、これを行う通常の方法は、カスタムのViewサブクラスを作成することです。 API Demosサンプルプロジェクトにはカスタムビューの例があり、Googleでたくさんのチュートリアルを見つけることができます。

26

私は同じ挑戦をしていました.Drawを上書きすることは少なくとも一般的にはうまくいかないと結論づけました。 My blogは理由を説明します。

  1. 新しい画像ビットマップを作成し、新しいキャンバスを付けてください。
  2. イメージビットマップをキャンバスに描画します。
  3. キャンバスに必要なものをすべて描画します。
  4. キャンバスをImageViewにアタッチします。ここで

、このためのコードスニペットです:

import android.graphics.Bitmap; 
import android.graphics.Canvas; 
import android.graphics.Paint; 
import android.graphics.RectF; 
import android.graphics.drawable.BitmapDrawable; 

ImageView myImageView = ... 
Bitmap myBitmap = ... 
Paint myRectPaint = ... 
int x1 = ... 
int y1 = ... 
int x2 = ... 
int y2 = ... 

//Create a new image bitmap and attach a brand new canvas to it 
Bitmap tempBitmap = Bitmap.createBitmap(myBitmap.getWidth(), myBitmap.getHeight(), Bitmap.Config.RGB_565); 
Canvas tempCanvas = new Canvas(tempBitmap); 

//Draw the image bitmap into the cavas 
tempCanvas.drawBitmap(myBitmap, 0, 0, null); 

//Draw everything else you want into the canvas, in this example a rectangle with rounded edges 
tempCanvas.drawRoundRect(new RectF(x1,y1,x2,y2), 2, 2, myPaint); 

//Attach the canvas to the ImageView 
myImageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap)); 
+0

ありがとう、うまく動作します – Lilo

+5

同じ問題を遭遇したときのちょっとした提案です。新しいBitmapDrawableを作成する代わりに、myImageView.setImageBitmap(tempBitmap)関数も使用できます。 – balachandarkm

20
 ImageView imageView=(ImageView) findViewById(R.id.image); 
     Bitmap bitmap = Bitmap.createBitmap(100, 100, Bitmap.Config.ARGB_8888);  
     Canvas canvas = new Canvas(bitmap); 
     Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG); 
     paint.setColor(Color.BLACK); 
     canvas.drawCircle(50, 50, 10, paint); 
     imageView.setImageBitmap(bitmap); 
+0

とsetImageBitmapの後にキャンバスを削除したい場合はどうすればよいですか? –

2

私はより良いアプローチは、カスタムImageViewのを作成し、onDrawメソッドをオーバーライドするだろうと思います。ような何か:

public class CustomView extends ImageView { 

public CustomView(Context context) { 
    super(context); 
} 

public CustomView(Context context, AttributeSet attrst) { 
    super(context, attrst); 
} 

public CustomView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
} 

MyBitmapFactory bitMapFac = null; 
public void setBitmapFactory(MyBitmapFactory bitMapFac) 
{ 
    this.bitMapFac = bitMapFac; 
} 

@Override 
public void onDraw(Canvas canvas) { 

    canvas.drawColor(Color.TRANSPARENT); 
    /*instantiate a bitmap and draw stuff here, it could well be another 
    class which you systematically update via a different thread so that you can get a fresh updated 
    bitmap from, that you desire to be updated onto the custom ImageView. 
    That will happen everytime onDraw has received a call i.e. something like:*/ 
    Bitmap myBitmap = bitMapFac.update(); //where update returns the most up to date Bitmap 
    //here you set the rectangles in which you want to draw the bitmap and pass the bitmap   
    canvas.drawBitmap(myBitMap, new Rect(0,0,400,400), new Rect(0,0,240,135) , null); 
    super.onDraw(canvas); 
    //you need to call postInvalidate so that the system knows that it should redraw your custom ImageView 
    this.postInvalidate(); 
} 
} 

onDraw内のコードは、すべての時間を実行しないように、アップデート経由()メソッドを取得するための新鮮なビットマップがあるかどうかを確認するいくつかのロジックを実装することをお勧めだろうとシステムにオーバーヘッドをかける。

そして、必要に応じてカスタム表示を使用します。最も簡単な方法は、次のようなactivity_layout.xml内に直接それを宣言するために、次のようになります。

<com.mycustomviews.CustomView 
     android:id="@+id/customView" 
     android:layout_centerInParent="true" 
     android:layout_height="135dp" 
     android:layout_width="240dp" 
     android:background="@android:color/transparent"/> 

そしてアクセスは、他のビューのようなあなたのコード内で使用して:

customView = (CustomView) findViewById(R.id.customView); 
関連する問題