custom image view android
My custom view looks as below
package com.mypackage;
import java.util.ArrayList;
import java.util.List;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.widget.ImageView;
public class CustomDrawableView extends ImageView {
// private ShapeDrawable mDrawable;
public int imageid = 0;
ShapeDrawable bgDrawable = null;
List<ShapeDrawable> ls = new ArrayList<ShapeDrawable>();
final int COUNT_SUMMERY = 3;
final int VERTICAL_OFFSET = 200;
final int HORIZENTAL_OFFSET = 20;
final int HORIZENTAL_GAP = 85;
final int VERTICAL_Y_POINT = 15;
final int VERTICAL_MAX_HEIGHT = 195;
final int HORIZENTAL_WIDTH = 60;
final int percentage[] = {25, 40, 35};
public CustomDrawableView(Context context, int id) {
super(context);
imageid = id;
switch(id) {
case R.drawable.summarychart:
for(int i = 0; i < COUNT_SUMMERY; i++) {
int x = HORIZENTAL_OFFSET + (HORIZENTAL_GAP * i);
int width = HORIZENTAL_WIDTH;
int height = VERTICAL_MAX_HEIGHT * percentage[i] / 100;
int y = VERTICAL_OFFSET + VERTICAL_Y_POINT + VERTICAL_MAX_HEIGHT - height;
ShapeDrawable objDrawable = new ShapeDrawable(new RectShape());
int color = 0;
switch(i) {
case 0:
color = Color.RED;
break;
case 1:
color = Color.GREEN;
break;
case 2:
color = Color.YELLOW;
break;
}
objDrawable.getPaint().setColor(color);
objDrawable.setBounds(x, y, x + width, y + height);
ls.add(objDrawable);
}
break;
default:
break;
}
int x = 0;
int y = 0;
int width = 320;
int height = 480;
bgDrawable = new ShapeDrawable(new RectShape());
bgDrawable.getPaint().setColor(0xffffffff);
bgDrawable.setBounds(x, y, x + width, y + height);
}
protected void onDraw(Canvas canvas) {
/* // Draw the white background
// bgDrawable.draw(canvas);
// Draw the bitmaps
Bitmap bmp = BitmapFactory.decodeResource(getResources(), imageid);
canvas.drawBitmap(bmp, 0, VERTICAL_OFFSET, null);
*/ // Draw bars
super.onDraw(canvas);
for(int i = 0; i < COUNT_SUMMERY; i++) {
ShapeDrawable objDrawable = ls.get(i);
objDrawable.draw(canvas);
}
}
}
And in the layout I have:
<com.mypackage.CustomDrawableView
android:layout_width="wrap_con开发者_如何学运维tent" android:layout_height="wrap_content"
android:src="@drawable/mychart" />
However I see the message:
NoSuchMethodException:com.mypackage.CustomDrawableView.(Android.context.Context, Android.util.AttributeSet)
and if I try to run it the application crashes. If I uncomment the drawable way, then it works, but other layouts are not displayed.
Any help is appreciated.
You can fix it by implementing your missing constructor. When you try to instantiate this from XML it is invoked.
public CustomDrawableView(Context context, AttributeSet attrs) {
super(context, attrs);
}
All you need to do is look at the exception you are seeing.
NoSuchMethodException:com.mypackage.CustomDrawableView.(Android.context.Context, Android.util.AttributeSet)
It means that there is no method with the signature com.mypackage.CustomDrawableView.(Android.context.Context, Android.util.AttributeSet)
because you haven't overridden it in your class.
精彩评论