I am trying to follow
http://developer.android.com/guide/topics/graphics/2d-graphics.html
and draw a shape on top of the imageview
my class is
package com.bayer.glucofacts;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.widget.ImageView;
public class CustomDrawableImageView extends ImageView {
private ShapeDrawable mDrawable;
public CustomDrawableImageView(Context context) {
super(context);
int x = 10;
int y = 100;
int width = 300;
int height = 50;
mDrawable = new ShapeDrawable(new OvalShape());
mDrawable.getPaint().setColor(0xff74AC23);
mDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
mDrawable.draw(canvas);
}
开发者_如何学Go}
and did xl like
<com.cmp.app.CustomDrawableImageView
android:id="@+id/bg_image" android:src="@drawable/book"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
but this crashes my app when i doo setContnetLayout in onCreate of the activity
when i was doing only ImageView without extending it for canvas drawing, it was working fine.
whats the problem ?
basically i plan to draw an image and a few lines on top of the image. how to do this ?
to solve this problem you must change your constructor to
public CustomDrawableImageView (Context context, AttributeSet attrs) {
super(context, attrs);
}
it will work
精彩评论