2015-05-26 112 views
0

基本上我想在圖像上繪製線條,以便當圖像放大時,線條也可以按比例放大,或者像放大或縮放重置等任何類型的交互。我使用TouchImageView作爲我的ImageView,這裏是我的代碼Android - 在TouchImageView上使用位圖繪製線條圖像查看

public class ParkA extends Activity{ 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.parka); 
     TouchImageView parka = (TouchImageView) findViewById(R.id.parka); 
     parka.setImageResource(R.drawable.parka); 

     Bitmap lineABmp = Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888); 
     Canvas lineACanvas = new Canvas(lineABmp); 
     Paint paint = new Paint(); 
     paint.setColor(Color.GREEN); 
     paint.setStrokeWidth(10); 
     lineACanvas.drawBitmap(lineABmp,0 ,0, null); 
     //lineACanvas.drawLine(60, 64, 60, 500, paint); 

     TouchImageView linea = new TouchImageView(this); 
     linea = (TouchImageView) findViewById(R.id.parka); 
     linea.setImageBitmap(lineABmp); 

    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 


    } 

    @Override 
    protected void onPause() { 


     super.onPause(); 
    } 

    @Override 
    protected void onNewIntent(Intent intent) { 


    } 

    @Override 
    public void onBackPressed() { 
     //Do Nothing 
    } 

} 

這裏是我的xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="com.kmparkit.ParkA" > 

    <com.km.parkit.TouchImageView 
     android:id="@+id/parka" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:contentDescription="@string/imageDesc" 
     android:scaleType="matrix" /> 
     <!-- android:src="@drawable/parka" --> 

</RelativeLayout> 

如果我改變Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);

進入Bitmap.createBitmap(parka.getWidth(), parka.getHeight(), Bitmap.Config.ARGB_8888);

的logcat的說寬度並且高度必須> 0

如果我畫線,只會顯示綠線本身

我在這裏丟失了什麼,以創建一個TouhImageView圖片上畫線?

更新:

下面是解決方案!

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.parka); 

     TouchImageView parka = (TouchImageView) findViewById(R.id.parka); 
     parka.setImageResource(R.drawable.parka); 

     Bitmap lineABmp = ((BitmapDrawable)parka.getDrawable()).getBitmap(); 
     Bitmap lineAMutBmp = lineABmp.copy(Bitmap.Config.ARGB_8888, true); 

     Canvas lineACanvas = new Canvas(lineAMutBmp); 
     Paint paint = new Paint(); 
     paint.setColor(Color.GREEN); 
     paint.setStrokeWidth(10); 
     lineACanvas.drawLine(82, 1058, 82, 858, paint); 

     parka.setImageBitmap(lineAMutBmp); 
    } 

我有位圖複製,以使其可變的,並且使用parka.setImageResource();parka.setImageBitmap()顯示圖片和一個TouchImageView同時使用Canvas繪製線的線。

回答

1

嘗試是這樣的:

編輯:這應該做的伎倆;)

setContentView(R.layout.parka); 
    TouchImageView parka = (TouchImageView) findViewById(R.id.parka); 
    parka.setImageResource(R.drawable.parka); 
    Bitmap lineABmp = ((BitmapDrawable)parka.getDrawable()).getBitmap(); 

    Bitmap copy = Bitmap.createBitmap(lineABmp); 

    Canvas lineACanvas = new Canvas(copy); 
    Paint paint = new Paint(); 
    paint.setColor(Color.GREEN); 
    paint.setStrokeWidth(10); 
    lineACanvas.drawLine(60, 64, 60, 500, paint); 

    parka.setImageBitmap(copy); 

快樂編碼:d

+0

的logcat的說,不可改變位圖傳遞到畫布構造.. –

+0

我改變了我的回答 – Gordak

+0

直到我對這個Bitmap lineABmp =((BitmapDrawable)parka.getDrawable())的Bitmap部分進行更改後,它仍然是一樣的。 \t位圖lineAMutBmp = lineABmp.copy(Bitmap.Config.ARGB_8888,true);'現在可行!但是隻顯示來自'setImageResource'的圖像,但是沒有顯示在畫布上:( –