39

私はAndroidプログラミングが初めてで、理解しようとしているのはこれです。

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

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

ありがとう!

4

5 に答える 5

34

私も同じ課題を抱えていましたが、onDrawの上書きは、少なくとも一般的なケースでは機能しないという結論に達しました。私のブログはその理由を説明しています。私にとって非常にうまくいったのは次のとおりです。

  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));
于 2012-06-11T18:58:59.407 に答える
33
     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);
于 2015-02-14T09:36:03.157 に答える
8

カスタム 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 内のコードが毎回実行されず、システムにオーバーヘッドがかからないように、 update() メソッドを介して取得する新しいビットマップがあるかどうかを確認するロジックを実装することをお勧めします。

そして、必要な場所でカスタム ビューを使用します。最も簡単な方法は、次のように 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);
于 2015-09-15T12:05:26.807 に答える
0

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

于 2011-02-07T04:53:41.570 に答える
0

すべての要素が垂直方向に配置されたレイアウト付きのxmlがある場合。

クラス ビューを拡張し、その onDraw メソッドをオーバーライドするクラスをパッケージ内に作成することで、目的を達成できます。好きなように円を描きます。

次に、レイアウトにimageViewを追加する代わりに、これを独自のビューをxmlレイアウトに追加します。次のように

< LinearLayout >

 < TextView> < /TextView>

  < Button> < /Button>

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

< /LinearLayout>

2D グラフィックについては、次のリンクを確認してください。読むのに最適なチュートリアルです。 http://organicandroid.blogspot.com/2010/08/starting-to-play-with-graphics.html この助けを願っています:)

于 2011-02-07T04:49:16.247 に答える