3

I created one class:

public class TestCanvas extends View {

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

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint();
        paint.setColor(Color.RED);

        canvas.drawText("kal", 0, 100, paint);
        canvas.save();
    }
}

Now I call that class from activity:

public class TestActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        TestCanvas tcanvas=new TestCanvas();

        frameLayout=(FrameLayout)findViewById(R.id.frameLayout1);
        frameLayout.addView(tcanvas);   
    }
}

Now I want to get canvas into activity class and set to ImageView. How would I do this?

Laurel
  • 5,965
  • 14
  • 31
  • 57
Kalpesh
  • 1,767
  • 19
  • 29
  • Do you want to set your canvas into some `ImageView` element? Or do you just want to display the canvas in the `R.id.frameLayout1`? – Jarno Argillander Nov 14 '11 at 13:39
  • I want to set canvas into one Imageview... – Kalpesh Nov 14 '11 at 18:16
  • Do you just want to draw on the screen inside a specific view? Because it doesn't have to be an ImageView, it can be any custom view. Why do you need it to be ImageView? Are there any special requirements for that? – Jarno Argillander Nov 14 '11 at 18:29

2 Answers2

5

You need to inherit your own class from View and override onDraw() and onMeasure(). Like you started to do with TestCanvas. An example:

package com.yourapphere;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.util.AttributeSet;
import android.view.View;

public class TwoDee extends View {
    private int mWidth;
    private int mHeight;


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

    public TwoDee(Context context, AttributeSet attribs) {
        super(context, attribs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        Paint paint = new Paint(); 
        paint.setColor(Color.GRAY); 
        paint.setStyle(Style.FILL); 
        canvas.drawPaint(paint);

        paint.setColor(Color.BLUE);
        canvas.drawLine(0, 0, mWidth, mHeight, paint);
        canvas.drawLine(mWidth, 0, 0, mHeight, paint);

        paint.setColor(Color.RED);
        canvas.drawText("0 kal", 50, 85, paint);
        canvas.save();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        mWidth = View.MeasureSpec.getSize(widthMeasureSpec);
        mHeight = View.MeasureSpec.getSize(heightMeasureSpec);
        setMeasuredDimension(mWidth, mHeight);
    }
}


Add your custom view into your activity's xml layout as below.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        <TextView  
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content" 
            android:text="@string/hello"
        />
        <com.yourapphere.TwoDee
            android:layout_width="150dp"
            android:layout_height="100dp"
        />
</LinearLayout>


Nothing goes into your activity class. That's it!

If you really need to use ImageView: inherit your custom view from ImageView instead of View. Then replace appropriate ImageView tags in your activity's layout xml with your custom ImageView like com.yourapphere.MyImageView)


References & links
See similar question: How to take canvas on imageview in android
Simple custom view example code: Adding my custom View into an XML layout throws exception

Read about Android 2D drawing: http://developer.android.com/guide/topics/graphics/2d-graphics.html
Android 2D coding tutorial: http://www3.ntu.edu.sg/home/ehchua/programming/android/Android_2D.html
Simple 2D game tutorial: http://www.barebonescoder.com/2010/06/android-development-simple-2d-graphics-part-1/

Community
  • 1
  • 1
Jarno Argillander
  • 5,885
  • 2
  • 31
  • 33
1

Here is a solution for those who having the same issue

public class TestActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TestCanvas tcanvas=new TestCanvas();

    frameLayout=(FrameLayout)findViewById(R.id.frameLayout1);
    frameLayout.addView(tcanvas);

    Bitmap bitmap=Bitmap.createBitmap(440,587,Bitmap.Config.ARGB_8888);
    Canvas c=new Canvas(bitmap);
    tcanvas.draw(bitmap);

    //now i use bitmap at for any use.......

  }
}  
Kalpesh
  • 1,767
  • 19
  • 29