我想繪製ImageView(就像一個簡單的繪畫,但只是在圖像上)。我創建了一個自定義的MyImageView類,它擴展了ImageView,我重寫了onDraw。我希望ImageView位於屏幕的中心。在ImageView上繪圖
當我使用一種佈局時,圖像出現在左上角,我可以在上面畫出沒有問題的圖像。
當我使用不同的佈局,圖像出現在屏幕中央但畫布仍然停留在左上角這個小的小矩形。因此,當我嘗試繪製圖像時,我只能在左上角進行繪製,並在中間的圖像上顯示變化。
在我的MainActivity我設置:
myImageView = (MyImageView) findViewById(R.id.pdfView);
// I also extract the bitmap myBitmap from the pdf ...
// and pass it to the ImageView
myImageView.setImageBitmap(myBitmap);
// and I set the Canvas too:
Canvas myCanvas = new Canvas(myBitmap);
myImageView.setMyCanvas(myCanvas);
然後這裏是MyImageView的關鍵部分:
public class MyImageView extends
android.support.v7.widget.AppCompatImageView {
// variables...
public MyImageView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
setupDrawing();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawPath(drawPath, drawPaint);
}
private void setupDrawing() { //...usual stuff here }
@Override
public boolean onTouchEvent(MotionEvent event) { //...usual stuff here }
public void setMyCanvas(Canvas canvas) { this.myCanvas = canvas; }
我一定要在MyImageView或添加/更改某事MainActivity,以便畫布僅位於ImageView的頂部或至少在整個屏幕上延伸?
你可以在這裏添加你的onDraw()方法的代碼。你有硬編碼的價值嗎?另外,第二種情況看起來像是將寬度和高度設置爲匹配圖像視圖的match_parent,這就是爲什麼您將整個區域作爲畫布。 – user2520215
我想加入 android:gravity =「center」 到你的線性佈局應該使它按照需要工作。 – user2520215