android - 如何获得 Canvas 区域点击?

标签 android view charts pie-chart

我已经使用 Canvas View 方法绘制了饼图,但现在我想要点击单个饼图?我怎样才能做到这一点? enter image description here

最佳答案

我得到了这个问题的完美答案:

获取点击区域的颜色代码,并检查颜色是否与您的颜色代码匹配,这将获得您想要的点击。

@Override
public boolean onTouchEvent(MotionEvent event) {

    float touchX = event.getX();

    float touchY = event.getY();

    Logger.debug("X-->"+touchX+" Y---->"+touchY);

    //get drawing cache of your view
    Bitmap bitmap = getDrawingCache(true);

            //Get color code of pixle where you have tap
    int colorCode=bitmap.getPixel((int)touchX,(int)touchY);

    if(colorCode == context.getResources().getColor(R.color.pie_blue)) {
        Logger.debug("Color blue");
        onPieClick.onBluePieClick(touchX,touchY);
    }else if(colorCode == context.getResources().getColor(R.color.pie_green)) {
        Logger.debug("Color green");
        onPieClick.onGreenPieClick(touchX,touchY);
    }

    return super.onTouchEvent(event);


}

关于android - 如何获得 Canvas 区域点击?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17469915/

相关文章:

android - TextToSpeech Android 的自定义语音

Android:CursorAdapter、ListView 和 CheckBox

android - 当用户选择组合框时在 webview 中弹出的 View 的样式

android - 在 Canvas 上绘制位图已缩放

c# - 如何为图表控件的 x 轴提供自定义标签?

php - 用 PHP 绘制 Google 图表

java - LIBGDX - 纹理故障

iOS 创建可重用 View 的问题

javascript - 如何在 Ember.js 中将特定元素添加到 DOM?

javascript - 带日期的 D3 平移图