android中饼图上的点击事件

发布于 2024-12-29 03:16:14 字数 2658 浏览 2 评论 0原文

我曾经使用画布绘制饼图.. 饼图中大约有 10 个弧线。我想对每个弧线执行单击事件。 有什么办法可以做到这一点吗?或者任何其他方式?

这是我的饼图视图..

MyView.java

package android.piechart;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {

private Paint p;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Integer> values;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    p = new Paint();
    p.setColor(Color.BLUE);
    p.setAntiAlias(true);

    colors = new ArrayList<Integer>();
    values = new ArrayList<Integer>();

    startX = 320 / 4;
    startY = 480 / 8;
    radius = 320 / 2;

    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.MAGENTA);
    colors.add(Color.BLUE);
    colors.add(Color.RED);

    values.add(0);
    values.add(1);
    values.add(3);
    values.add(0);
    values.add(2);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Log.e("", "onDraw() is called...");

    float offset = 0;
    float sum = 0;
    for (int a = 0; a < values.size(); a++) {
        sum += values.get(a);
    }

    float angle = (float) (360 / sum);

    Log.e("angle", "" + angle);

    RectF rectF = new RectF();
    rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
            getStartY() + getRadius());

    for (int i = 0; i < values.size(); i++) {

        p.setColor(colors.get(i));

        if (i == 0) {
            canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
        } else {
            canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
        }

        offset += (values.get(i) * angle);
    }

    canvas.save();
}

public int getStartX() {
    return startX;
}

public void setStartX(int startX) {
    this.startX = startX;
}

public int getStartY() {
    return startY;
}

public void setStartY(int startY) {
    this.startY = startY;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}

public ArrayList<Integer> getColors() {
    return colors;
}

public void setColors(ArrayList<Integer> colors) {
    this.colors = colors;
}

public ArrayList<Integer> getValues() {
    return values;
}

public void setValues(ArrayList<Integer> values) {
    this.values = values;
}
}

提前致谢..

I have used to draw pie chart using canvas..
There are approximately 10 arcs in pie chart..i want to perform click event on each arc.
Is there any way to do this? or any other way?

This is my pie chart view..

MyView.java

package android.piechart;
import java.util.ArrayList;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

public class MyView extends View {

private Paint p;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Integer> values;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);
    p = new Paint();
    p.setColor(Color.BLUE);
    p.setAntiAlias(true);

    colors = new ArrayList<Integer>();
    values = new ArrayList<Integer>();

    startX = 320 / 4;
    startY = 480 / 8;
    radius = 320 / 2;

    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.MAGENTA);
    colors.add(Color.BLUE);
    colors.add(Color.RED);

    values.add(0);
    values.add(1);
    values.add(3);
    values.add(0);
    values.add(2);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);

    Log.e("", "onDraw() is called...");

    float offset = 0;
    float sum = 0;
    for (int a = 0; a < values.size(); a++) {
        sum += values.get(a);
    }

    float angle = (float) (360 / sum);

    Log.e("angle", "" + angle);

    RectF rectF = new RectF();
    rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
            getStartY() + getRadius());

    for (int i = 0; i < values.size(); i++) {

        p.setColor(colors.get(i));

        if (i == 0) {
            canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
        } else {
            canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
        }

        offset += (values.get(i) * angle);
    }

    canvas.save();
}

public int getStartX() {
    return startX;
}

public void setStartX(int startX) {
    this.startX = startX;
}

public int getStartY() {
    return startY;
}

public void setStartY(int startY) {
    this.startY = startY;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}

public ArrayList<Integer> getColors() {
    return colors;
}

public void setColors(ArrayList<Integer> colors) {
    this.colors = colors;
}

public ArrayList<Integer> getValues() {
    return values;
}

public void setValues(ArrayList<Integer> values) {
    this.values = values;
}
}

Thanks in advance..

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

原来分手还会想你 2025-01-05 03:16:14

我自己解决了我的问题...

MyView.java

public class MyView extends View {

private Paint p;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Float> values;
Bitmap bitmap;
Context mContext;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mContext = context;

    p = new Paint();
    p.setAntiAlias(true);

    colors = new ArrayList<Integer>();
    values = new ArrayList<Float>();

    startX = 320 / 4;
    startY = 480 / 8;
    radius = 320 / 2;

    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.MAGENTA);
    colors.add(Color.BLUE);
    colors.add(Color.RED);

    values.add(5f);
    values.add(1f);
    values.add(3f);
    values.add(5f);
    values.add(2f);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bitmap);

    Log.e("", "onDraw() is called...");

    float offset = 0;
    float sum = 0;
    for (int a = 0; a < values.size(); a++) {
        sum += values.get(a);
    }

    float angle = (float) (360 / sum);

    Log.e("angle", "" + angle);

    RectF rectF = new RectF();
    rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
            getStartY() + getRadius());

    for (int i = 0; i < values.size(); i++) {

        p.setColor(colors.get(i));

        if (i == 0) {
            canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
            c.drawArc(rectF, 0, values.get(i) * angle, true, p);
        } else {
            canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
            c.drawArc(rectF, offset, values.get(i) * angle, true, p);
        }

        offset += (values.get(i) * angle);
    }

    canvas.save();

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    int color = bitmap.getPixel((int) event.getX(), (int) event.getY());

    Log.e("", "" + color);

    if (colors.contains(color)) {
        Log.e("", "is matching");
        if (color == Color.RED) {
            Toast.makeText(mContext, "Is Red", Toast.LENGTH_SHORT).show();
        }

        if (color == Color.CYAN) {
            Toast.makeText(mContext, "Is Cyan", Toast.LENGTH_SHORT).show();
        }

        if (color == Color.MAGENTA) {
            Toast.makeText(mContext, "Is MAGENTA", Toast.LENGTH_SHORT)
                    .show();
        }
        if (color == Color.BLUE) {
            Toast.makeText(mContext, "Is BLUE", Toast.LENGTH_SHORT).show();
        }
        if (color == Color.GREEN) {
            Toast.makeText(mContext, "Is GREEN", Toast.LENGTH_SHORT).show();
        }
    }

    return super.onTouchEvent(event);
}

public int getStartX() {
    return startX;
}

public void setStartX(int startX) {
    this.startX = startX;
}

public int getStartY() {
    return startY;
}

public void setStartY(int startY) {
    this.startY = startY;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}

public ArrayList<Integer> getColors() {
    return colors;
}

public void setColors(ArrayList<Integer> colors) {
    this.colors = colors;
}

public ArrayList<Float> getValues() {
    return values;
}

public void setValues(ArrayList<Float> values) {
    this.values = values;
}

}

我希望它对其他人有用...

I solved my question myself...

MyView.java

public class MyView extends View {

private Paint p;
private int startX;
private int startY;
private int radius;
private ArrayList<Integer> colors;
private ArrayList<Float> values;
Bitmap bitmap;
Context mContext;

public MyView(Context context, AttributeSet attrs) {
    super(context, attrs);

    mContext = context;

    p = new Paint();
    p.setAntiAlias(true);

    colors = new ArrayList<Integer>();
    values = new ArrayList<Float>();

    startX = 320 / 4;
    startY = 480 / 8;
    radius = 320 / 2;

    colors.add(Color.GREEN);
    colors.add(Color.CYAN);
    colors.add(Color.MAGENTA);
    colors.add(Color.BLUE);
    colors.add(Color.RED);

    values.add(5f);
    values.add(1f);
    values.add(3f);
    values.add(5f);
    values.add(2f);

}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(),
            Bitmap.Config.ARGB_8888);

    Canvas c = new Canvas(bitmap);

    Log.e("", "onDraw() is called...");

    float offset = 0;
    float sum = 0;
    for (int a = 0; a < values.size(); a++) {
        sum += values.get(a);
    }

    float angle = (float) (360 / sum);

    Log.e("angle", "" + angle);

    RectF rectF = new RectF();
    rectF.set(getStartX(), getStartY(), getStartX() + getRadius(),
            getStartY() + getRadius());

    for (int i = 0; i < values.size(); i++) {

        p.setColor(colors.get(i));

        if (i == 0) {
            canvas.drawArc(rectF, 0, values.get(i) * angle, true, p);
            c.drawArc(rectF, 0, values.get(i) * angle, true, p);
        } else {
            canvas.drawArc(rectF, offset, values.get(i) * angle, true, p);
            c.drawArc(rectF, offset, values.get(i) * angle, true, p);
        }

        offset += (values.get(i) * angle);
    }

    canvas.save();

}

@Override
public boolean onTouchEvent(MotionEvent event) {

    int color = bitmap.getPixel((int) event.getX(), (int) event.getY());

    Log.e("", "" + color);

    if (colors.contains(color)) {
        Log.e("", "is matching");
        if (color == Color.RED) {
            Toast.makeText(mContext, "Is Red", Toast.LENGTH_SHORT).show();
        }

        if (color == Color.CYAN) {
            Toast.makeText(mContext, "Is Cyan", Toast.LENGTH_SHORT).show();
        }

        if (color == Color.MAGENTA) {
            Toast.makeText(mContext, "Is MAGENTA", Toast.LENGTH_SHORT)
                    .show();
        }
        if (color == Color.BLUE) {
            Toast.makeText(mContext, "Is BLUE", Toast.LENGTH_SHORT).show();
        }
        if (color == Color.GREEN) {
            Toast.makeText(mContext, "Is GREEN", Toast.LENGTH_SHORT).show();
        }
    }

    return super.onTouchEvent(event);
}

public int getStartX() {
    return startX;
}

public void setStartX(int startX) {
    this.startX = startX;
}

public int getStartY() {
    return startY;
}

public void setStartY(int startY) {
    this.startY = startY;
}

public int getRadius() {
    return radius;
}

public void setRadius(int radius) {
    this.radius = radius;
}

public ArrayList<Integer> getColors() {
    return colors;
}

public void setColors(ArrayList<Integer> colors) {
    this.colors = colors;
}

public ArrayList<Float> getValues() {
    return values;
}

public void setValues(ArrayList<Float> values) {
    this.values = values;
}

}

I hope it's useful to others...

七分※倦醒 2025-01-05 03:16:14

如果您使用过 Canvas,您可能会参考它的底层位图。您可以使用 Bitmap.getPixel(int x, int y) 并测试它返回的颜色来确定点击位于哪个部分。

否则,您必须手动进行计算以找出哪个楔形包含您点击的 x,y 坐标。

If you've used a Canvas, you probably have a reference to its underlying Bitmap. You could use Bitmap.getPixel(int x, int y) and test the color it returns to decide which section the click was in.

Otherwise, you have to do the calculation manually to figure out which wedge contains the x,y coordinates of your click.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文