自定义图像视图android [英] custom image view android

查看:92
本文介绍了自定义图像视图android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的自定义视图如下

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_content" android:layout_height="wrap_content"
        android:src="@drawable/mychart" />

但是我看到消息:

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

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.

感谢您的帮助.

推荐答案

您可以通过实现缺少的构造函数来修复它.当您尝试从XML实例化该对象时.

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

这篇关于自定义图像视图android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆