Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.68 KB | None | 0 0
  1. <rotate
  2. android:fromDegrees="45"
  3. android:toDegrees="45"
  4. android:pivotX="-40%"
  5. android:pivotY="87%" />
  6.  
  7. public class TriangleButton extends Button {
  8.  
  9. private final float RADIUS = 50.0f;
  10.  
  11. private boolean mIsPressed;
  12.  
  13. private Region mPathRegion;
  14. private final Path mPath = new Path();
  15.  
  16. private final Paint mNormalPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  17. {
  18. setDither(true);
  19. setStyle(Style.FILL);
  20. setPathEffect(new CornerPathEffect(RADIUS));
  21. }
  22. };
  23.  
  24. private final Paint mPressedPaint = new Paint(Paint.ANTI_ALIAS_FLAG) {
  25. {
  26. setDither(true);
  27. setStyle(Style.FILL);
  28. setPathEffect(new CornerPathEffect(RADIUS));
  29. }
  30. };
  31.  
  32. public TriangleButton(final Context context) {
  33. this(context, null);
  34. }
  35.  
  36. public TriangleButton(final Context context, final AttributeSet attrs) {
  37. this(context, attrs, 0);
  38. }
  39.  
  40. public TriangleButton(final Context context, final AttributeSet attrs, final int defStyleAttr) {
  41. super(context, attrs, defStyleAttr);
  42.  
  43. setWillNotDraw(false);
  44. setBackgroundColor(Color.TRANSPARENT);
  45. setColors(Color.RED, Color.GRAY);
  46. }
  47.  
  48. public void setColors(final int color, final int pressed) {
  49. mNormalPaint.setColor(color);
  50. mPressedPaint.setColor(pressed);
  51. }
  52.  
  53. @Override
  54. protected void onSizeChanged(final int w, final int h, final int oldw, final int oldh) {
  55. super.onSizeChanged(w, h, oldw, oldh);
  56.  
  57. // Set triangle
  58. mPath.reset();
  59. mPath.moveTo(0.0f, h);
  60. mPath.lineTo(w / 2.0f, 0.0f);
  61. mPath.lineTo(w, h);
  62. mPath.close();
  63.  
  64. // Create region for touch detecting
  65. final RectF rectF = new RectF();
  66. mPath.computeBounds(rectF, true);
  67. mPathRegion = new Region();
  68. mPathRegion.setPath(
  69. mPath,
  70. new Region(
  71. (int) rectF.left,
  72. (int) rectF.top,
  73. (int) rectF.right,
  74. (int) rectF.bottom)
  75. );
  76. }
  77.  
  78. @Override
  79. public boolean onTouchEvent(MotionEvent event) {
  80. if (mPathRegion.contains((int) event.getX(), (int) event.getY())) {
  81. switch (event.getAction()) {
  82. case MotionEvent.ACTION_DOWN:
  83. mIsPressed = true;
  84. break;
  85. case MotionEvent.ACTION_UP:
  86. mIsPressed = false;
  87. performClick();
  88. break;
  89. case MotionEvent.ACTION_CANCEL:
  90. mIsPressed = false;
  91. break;
  92. }
  93. postInvalidate();
  94. } else {
  95. mIsPressed = false;
  96. postInvalidate();
  97. return false;
  98. }
  99.  
  100. postInvalidate();
  101. return true;
  102. }
  103.  
  104. @Override
  105. protected void onDraw(final Canvas canvas) {
  106. canvas.drawPath(mPath, mIsPressed ? mPressedPaint : mNormalPaint);
  107. super.onDraw(canvas);
  108. }
  109. }
  110.  
  111. <FrameLayout
  112. android:layout_width="match_parent"
  113. android:layout_height="match_parent">
  114.  
  115. <com.example.user.timezonetest.TriangleButton
  116. android:id="@+id/btn_bottom"
  117. android:layout_width="300dp"
  118. android:layout_height="250dp"
  119. android:text="Second Button"
  120. android:textColor="#000"
  121. android:layout_gravity="center_horizontal"
  122. android:gravity="bottom|center_horizontal"
  123. android:padding="30dp"/>
  124.  
  125. <com.example.user.timezonetest.TriangleButton
  126. android:id="@+id/btn_top"
  127. android:layout_width="180dp"
  128. android:layout_height="160dp"
  129. android:text="First Button"
  130. android:textColor="#000"
  131. android:layout_gravity="center_horizontal"
  132. android:gravity="bottom|center_horizontal"
  133. android:layout_margin="30dp"
  134. android:padding="30dp"/>
  135.  
  136. </FrameLayout>
  137.  
  138. final TriangleButton bottomButton = (TriangleButton) findViewById(R.id.btn_bottom);
  139. final TriangleButton topButton = (TriangleButton) findViewById(R.id.btn_top);
  140.  
  141. bottomButton.setColors(Color.RED, Color.GRAY);
  142. topButton.setColors(Color.YELLOW, Color.GRAY);
  143.  
  144. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
  145. android:layout_width="300dp"
  146. android:layout_height="300dp" >
  147.  
  148. <Button
  149. android:layout_width="300dp"
  150. android:layout_height="300dp"
  151. android:background="@drawable/rectangle_1" />
  152.  
  153. <Button
  154. android:layout_width="150dp"
  155. android:layout_height="150dp"
  156. android:background="@drawable/rectangle_2" />
  157. />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement