2017-11-11 291 views
11

我正在創建自定義弧視圖,這與彩虹視圖類似。我可以繪製弧形視圖,但我無法爲每個視圖創建單獨的點擊事件。如何爲每個弧視圖設置單獨的點擊事件?提前致謝。繪製自定義弧視圖並檢測每條弧線的用戶點擊

下面是代碼:

ArcView.java

public class ArcView extends View implements View.OnTouchListener{ 

    Paint paint; 

    int radius, x, y; 
    int color; 

    public ArcView(Context context) { 
     super(context); 
     init(); 
    } 

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

    public ArcView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     init(); 
    } 

    public ArcView(Context context, int radius, int x, int y, int color) { 
     super(context); 

     this.radius = radius; 
     this.x = x; 
     this.y = y; 
     this.color = color; 
     init(); 
    } 

    private void init(){ 
     paint = new Paint(); 
     paint.setColor(color); 
     paint.setStrokeWidth(10); 
     paint.setStyle(Paint.Style.STROKE); 
     setOnTouchListener(this); 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     super.onDraw(canvas); 
     paint.setStyle(Paint.Style.FILL); 
     canvas.drawCircle(x, y, radius, paint); 
    } 

    @Override 
    public boolean onTouch(View view, MotionEvent event) { 
     return false; 
    } 
} 

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    RelativeLayout arcButton; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     arcButton = (RelativeLayout) findViewById(R.id.arcButton); 
     arcButton1 = (RelativeLayout) findViewById(R.id.arcButton1); 
     arcButton2 = (RelativeLayout) findViewById(R.id.arcButton2); 

     DisplayMetrics dm = new DisplayMetrics(); 
     getWindowManager().getDefaultDisplay().getMetrics(dm); 
     int width=dm.widthPixels; 
     int height=dm.heightPixels; 

     int arcRadius1 = (int)(width/1.5); 
     int arcRadius2 = arcRadius1+(int)(width/3.5); 
     int arcRadius3 = arcRadius2+(int)(width/3.5); 

     int xCoor = width/2; 
     int yCoor = height; 

     RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height); 

     arcButton.setLayoutParams(params); 
     View arcView1=new ArcView(this, arcRadius3, xCoor, yCoor, Color.RED); 
     View arcView2=new ArcView(this, arcRadius2, xCoor, yCoor, Color.BLACK); 
     View arcView3=new ArcView(this, arcRadius1, xCoor, yCoor, Color.BLUE); 
     arcButton.addView(arcView1); 
     arcButton1.addView(arcView2); 
     arcButton2.addView(arcView3); 

    } 
} 

輸出:

Output screen

如何爲每個弧形按鈕創建單獨的單擊事件?

+1

你不能做到這一點 - 而不是做一個自定義的'View'有幾個「圈」 - 每一個與自己的點擊收聽 – pskink

+0

@ pskink感謝您的快速回復。我會試試這個。 – VigneshK

+0

我試圖分別創建三個環,並創建每個點擊偵聽器,但它將採用最後一個視圖的點擊事件。 – VigneshK

回答

9

有點解決方法,但它的工作原理。將這個裏面ArcView

@Override 
public boolean performClick() { 
    return super.performClick(); 
} 

@Override 
public boolean onTouch(View view, MotionEvent event) { 
    view.performClick(); 
    if (isPointInCircle((int) event.getX(), (int) event.getY())) { 
     // do what you want to do and get rid of the next two lines 
     String color = (((ArcView) view).color == Color.RED) ? "RED" : (((ArcView) view).color == Color.BLACK) ? "BLACK" : "BLUE"; 
     Toast.makeText(context, color, Toast.LENGTH_SHORT).show(); 
     return true; 
    } 
    return false; 
} 

private boolean isPointInCircle(int clickX, int clickY) { 
    return (clickX - x) * (clickX - x) + (clickY - y) * (clickY - y) <= radius * radius; 
} 

注意,對於Toast工作,你需要做context全球性的,但最有可能你會希望得到反正擺脫Toast的。

enter image description here