自定义图像视图android

时间:2010-07-16 14:57:56

标签: java android imageview

我的自定义视图如下所示


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

在布局中我有:

<com.mypackage.CustomDrawableView
        android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:src="@drawable/mychart" />

但是我看到了这样的信息:

  

NoSuchMethodException:com.mypackage.CustomDrawableView。(Android.context.Context,Android.util.AttributeSet)

如果我尝试运行它,应用程序崩溃了。如果我取消注释drawable方式,那么它可以工作,但不显示其他布局。

感谢任何帮助。

2 个答案:

答案 0 :(得分:3)

您可以通过实现缺少的构造函数来修复它。当您尝试从XML实例化它时,它将被调用。

public CustomDrawableView(Context context, AttributeSet attrs) {
    super(context, attrs);
}

答案 1 :(得分:1)

您需要做的就是查看您所看到的异常。

  

NoSuchMethodException:com.mypackage.CustomDrawableView(Android.context.Context,   Android.util.AttributeSet)

这意味着没有签名com.mypackage.CustomDrawableView.(Android.context.Context, Android.util.AttributeSet)的方法,因为您没有在班级中覆盖它。