Advertisement
Guest User

Untitled

a guest
Mar 30th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package com.innercirclesoftware.drawabletest;
  2.  
  3. import android.content.Context;
  4. import android.graphics.Outline;
  5. import android.graphics.Path;
  6. import android.graphics.Rect;
  7. import android.graphics.Region;
  8. import android.support.annotation.NonNull;
  9. import android.support.annotation.Nullable;
  10. import android.support.v7.widget.AppCompatImageButton;
  11. import android.util.AttributeSet;
  12. import android.view.MotionEvent;
  13. import android.view.View;
  14. import android.view.ViewOutlineProvider;
  15.  
  16. public class DiamondImageButton extends AppCompatImageButton {
  17.  
  18.     @Nullable private Region clickRegion;
  19.  
  20.     public DiamondImageButton(Context context) {
  21.         super(context);
  22.         init();
  23.     }
  24.  
  25.     public DiamondImageButton(Context context, AttributeSet attrs) {
  26.         super(context, attrs);
  27.         init();
  28.     }
  29.  
  30.     public DiamondImageButton(Context context, AttributeSet attrs, int defStyleAttr) {
  31.         super(context, attrs, defStyleAttr);
  32.         init();
  33.     }
  34.  
  35.     private void init() {
  36.         setOutlineProvider(new ViewOutlineProvider() {
  37.             @Override
  38.             public void getOutline(View view, Outline outline) {
  39.                 int left = 0;
  40.                 int top = 0;
  41.                 int right = view.getWidth();
  42.                 int bottom = view.getHeight();
  43.                 int centerX = (left + right) / 2;
  44.                 int centerY = (top + bottom) / 2;
  45.  
  46.                 Path path = new Path();
  47.                 path.moveTo(centerX, top);
  48.                 path.lineTo(left, centerY);
  49.                 path.lineTo(centerX, bottom);
  50.                 path.lineTo(right, centerY);
  51.                 path.close();
  52.  
  53.                 setClickRegion(path);
  54.  
  55.                 outline.setConvexPath(path);
  56.             }
  57.         });
  58.     }
  59.  
  60.     private void setClickRegion(@NonNull Path clipPath) {
  61.         Rect clipBounds = new Rect(0, 0, getWidth(), getHeight());
  62.  
  63.         this.clickRegion = new Region();
  64.         this.clickRegion.setPath(clipPath, new Region(clipBounds));
  65.     }
  66.  
  67.     @Override
  68.     public boolean onTouchEvent(MotionEvent event) {
  69.         int x = (int) event.getX();
  70.         int y = (int) event.getY();
  71.  
  72.         if (clickRegion != null && !clickRegion.contains(x, y)) {
  73.             //clicked outside the diamond
  74.             return false;
  75.         }
  76.  
  77.         return super.onTouchEvent(event);
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement