Advertisement
Guest User

Untitled

a guest
Feb 25th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. public class HelloCustomView extends View {
  2. String first;
  3. String second;
  4.  
  5. public HelloCustomView(Context context, AttributeSet attrs) {
  6. super(context, attrs);
  7.  
  8.  
  9. TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.HelloCustomView, 0, 0);
  10.  
  11. first = a.getString(R.styleable.HelloCustomView_firstText);
  12. second = a.getString(R.styleable.HelloCustomView_secondText);
  13.  
  14. a.recycle();
  15. every5Secondes();
  16. }
  17.  
  18. Paint bgpaint = new Paint();
  19. Paint fgpaint = new Paint();
  20.  
  21. @Override
  22. protected void onDraw(Canvas canvas) {
  23. bgpaint.setStyle(Paint.Style.FILL);
  24. bgpaint.setColor(Color.RED);
  25.  
  26. canvas.drawRect(0, 0, getWidth(), getHeight(), bgpaint);
  27.  
  28. fgpaint.setColor(Color.BLACK);
  29. fgpaint.setTextSize(100);
  30.  
  31. // String text = isAltText ? "Custom" : "Hello";
  32. String text = isAltText ? second : first;
  33.  
  34. canvas.drawText(text, 10, 100, fgpaint);
  35. }
  36.  
  37. @Override
  38. public boolean onTouchEvent(MotionEvent event) {
  39. if(event.getAction() == MotionEvent.ACTION_DOWN) {
  40. Toast.makeText(getContext(), "touched!", Toast.LENGTH_LONG).show();
  41. return true;
  42. }
  43. return super.onTouchEvent(event);
  44. }
  45.  
  46. Handler handler = new Handler();
  47. boolean isAltText = false;
  48.  
  49. void every5Secondes() {
  50. isAltText = !isAltText;
  51. invalidate();
  52.  
  53. handler.postDelayed(new Runnable() {
  54. @Override
  55. public void run() {
  56. every5Secondes();
  57. }
  58. }, 5000);
  59. }
  60.  
  61.  
  62.  
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement