Kevin Murvie
Kevin Murvie

Reputation: 2652

Android - Drawing lines using bitmap on TouchImageView

Basically I would like to draw lines on an image so that when that image is zoomed, the lines would also get zoomed proportionally, or any kind of interactivity like fling or zoom reset. I'm using TouchImageView as my ImageView and here's my code

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
    }

}

Here's my xml file

<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>

If I change the Bitmap.createBitmap(480, 640, Bitmap.Config.ARGB_8888);

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

The logcat says that width and height must be > 0

If I drawLine, only the green line itself would be shown

What am I missing here in order to create a TouhImageView picture with lines drawn on it?

UPDATE :

Here's the solution!

@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);
    }

I have to copy the bitmap in order to make it mutable and use parka.setImageResource(); and parka.setImageBitmap() to display the picture and the lines in one TouchImageView while drawing the lines using Canvas.

Upvotes: 1

Views: 485

Answers (1)

Gordak
Gordak

Reputation: 2070

Try something like this:

EDIT : this should do the trick ;)

    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);

Happy coding :D

Upvotes: 1

Related Questions