Search code examples
androidviewpaintsetcontentview

Android: setContentView for a painting page


Hi I am learning on making a paint app where users can paint lines on a picture. I have followed the fingerpaint tutorial on the web, and the onCreate part is as follows:

public class ColoringPageActivity2 extends FragmentActivity implements ColorPickerDialog.OnColorChangedListener 
{    
    private Paint mPaint;
    private MaskFilter mEmboss;
    private MaskFilter mBlur;
    MyView mv;
    AlertDialog dialog;
    ActionBar actionBar;
    Dialog dialog_draw;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
        setContentView(R.layout.coloring_activity2);
        mv = (MyView) findViewById(R.id.drawerView);      //replaced setContentView(mv);
        mv.setDrawingCacheEnabled(true);
        mv.setBackgroundResource(R.drawable.ic_stub);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setDither(true);
        mPaint.setColor(0xFFFF0000);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setStrokeJoin(Paint.Join.ROUND);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(20);
    }

Continue inside the same class: ColoringPageActivity2

public class MyView extends View 
{
    private static final float MINP = 0.25f;
    private static final float MAXP = 0.75f;
    private Bitmap mBitmap;
    private Canvas mCanvas;
    private Path mPath;
    private Paint mBitmapPaint;
    Context context;

    public MyView(Context c) 
    {
        super(c);
        context = c;
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) 
    {
         ....

coloring_activity.xml:

 <RelativeLayout
            android:id="@+id/drawing_panel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/layout_tool"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true" >

            <ImageView
                android:id="@+id/image_color"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:src="@drawable/ic_stub" />

            <com.abc.abc.activity.ColoringPageActivity2.MyView
                android:id="@+id/drawerView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@android:color/transparent" />
        </RelativeLayout>

Logcat:

10-21 00:15:37.685: E/AndroidRuntime(24558): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.abc.abc.activity.ColoringPageActivity2.MyView" on path: /data/app/com.abc.abc-2.apk
10-21 00:15:37.685: E/AndroidRuntime(24558):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:64)
10-21 00:15:37.685: E/AndroidRuntime(24558):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
10-21 00:15:37.685: E/AndroidRuntime(24558):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
10-21 00:15:37.685: E/AndroidRuntime(24558):    at android.view.LayoutInflater.createView(LayoutInflater.java:565)
10-21 00:15:37.685: E/AndroidRuntime(24558):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:702)
10-21 00:15:37.685: E/AndroidRuntime(24558):    ... 23 more

Question:

following the fingerpaint example, originally it was setContentView(mv) and mv = new MyView(this);, which was a full screen painting page. Instead i would like to add some buttons at the bottom, so i have designed a customized xml and to fit the MyView into the xml.

Pressing the <com.abc.abc.activity.ColoringPageActivity2.MyView can directly jump to the MyView in the ColoringActivity2` class.

However, the above logcat is pop out when running the ColoringPageActivity2.

How could that be solved?

Thanks!


Solution

  • If you put MyView Class in a separated class, it will work. Just as a Button or a TextView that are separated classes, it will work for sure if your CustomView is a separeted class. And it is easy to call in another Activity if you need it.