在Canvas.draw中使用drawable png的Android Custom Class

时间:2017-09-21 02:10:24

标签: android canvas drawable ondraw

我已经关注了一些论坛,我已经迷失了这个问题:

我有一个自定义视图,为我绘制了9个圆圈,如下所示:

enter image description here

这个圆圈必须是主要的一个图标,另一个是8个图像,它必须是一个可绘制的资源(我的可绘制文件夹中有png图片)。

如何设置此可绘制图像而不是颜色?

这是我的实际代码:

public class CircleView  extends View {

public static interface IMenuListener{

    public void onMenuClick(MenuCircle item);
}

public static class MenuCircle{
    private int x,y,radius;
    public int id;
    public String text;

}

private Paint mainPaint;
private Paint secondPaint;
private Paint textPaint;
private int radius_main =130;

private int menuInnerPadding = 40;
private int radialCircleRadius = 60;
private int textPadding = 25;
private double startAngle = - Math.PI/2f;
private ArrayList<MenuCircle> elements;
private IMenuListener listener;

public void setListener(IMenuListener listener){
    this.listener = listener;
}
public void clear(){
    elements.clear();
    listener=null;
}
public CircleView(Context context) {
    super(context);
    init();
}

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

public CircleView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init();
}
private void init(){
    elements = new ArrayList<>();
}
public void addMenuItem(String text,int id){
    MenuCircle item = new MenuCircle();
    item.id = id;
    item.text=text;
    elements.add(item);

}


@Override
protected void onFinishInflate() {
    super.onFinishInflate();

    mainPaint = new Paint();
    mainPaint.setColor(Color.DKGRAY); //color of main circle
    secondPaint = new Paint();
    secondPaint.setColor(Color.DKGRAY); // color of 8 circles
    textPaint = new Paint();
    textPaint.setColor(Color.BLACK); // text under 8 circles
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    int centerX = canvas.getWidth()/2 ;
    int centerY= canvas.getHeight()/2;
    canvas.drawCircle(centerX,centerY,radius_main,mainPaint);

    for(int i=0;i<elements.size();i++){
        double angle =0;
        if(i==0){
            angle = startAngle;
        }else{
            angle = startAngle+(i * ((2 * Math.PI) / elements.size()));
        }
        elements.get(i).x = (int) (centerX + Math.cos(angle)*(radius_main+menuInnerPadding+radialCircleRadius));
        elements.get(i).y = (int) (centerY + Math.sin(angle)*(radius_main+menuInnerPadding+radialCircleRadius));


        canvas.drawCircle( elements.get(i).x,elements.get(i).y,radialCircleRadius,secondPaint);

        float tW = textPaint.measureText(elements.get(i).text);
        canvas.drawText(elements.get(i).text,elements.get(i).x-tW/2,elements.get(i).y+radialCircleRadius+textPadding,textPaint);
    }

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if(event.getAction()==MotionEvent.ACTION_DOWN){
        for(MenuCircle mc : elements){
            double distance =  Math.hypot(event.getX()-mc.x,event.getY()-mc.y);
            if(distance<= radialCircleRadius){
                //touched
                if(listener!=null)
                    listener.onMenuClick(mc);
                return true;
            }
        }

    }

    return super.onTouchEvent(event);
}

@Override
protected void onDetachedFromWindow() {
    super.onDetachedFromWindow();

}
}

0 个答案:

没有答案