【Android】旋轉的圓形按鈕
阿新 • • 發佈:2019-01-24
以前想到的按鈕樣式,學了一段時間Android後終於有能力實現了。
點選白色螢幕就旋轉繪製一個按鈕,隨機顏色和文字。按按鈕彈出訊息框顯示你按下的按鈕。
Button.java
package com.example.button; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Path; import android.graphics.Rect; import android.graphics.RectF; import android.graphics.Typeface; import android.util.Log; public class Button{ Paint paint; private int status = 0; private float rotate = 0.0f; float x;//160 float y;//100 float r;//40 int timepan = 50; String data;//"open" String color; float strwid; float strhei ; // Drawthread t; public Button(float x,float y,float r,String data,String color) { // this.setOnTouchListener(null); // setFocusable(true); // setFocusableInTouchMode(true); this.x = x; this.y = y; this.r = r; this.data = data; this.color = color; // t = new Drawthread(canvas); // t.start(); paint = new Paint(Paint.ANTI_ALIAS_FLAG); paint.setTextSize(24); paint.setTypeface(Typeface.DEFAULT_BOLD); Rect rect = new Rect(); paint.getTextBounds(data, 0, data.length(), rect); strwid = rect.width(); strhei = rect.height(); // paint.setStrokeWidth(10); paint.setColor(Color.parseColor(color));//"#00e9ff" } public void getArc(Canvas canvas,float o_x,float o_y,float r, float startangel,float endangel,Paint paint){ RectF rect = new RectF(o_x - r, o_y - r, o_x + r, o_y + r); Path path = new Path(); path.moveTo(o_x,o_y); path.lineTo((float)(o_x+r*Math.cos(startangel*Math.PI/180)) , (float)(o_y+r*Math.sin(startangel*Math.PI/180))); path.lineTo((float)(o_x+r*Math.cos(endangel*Math.PI/180)) , (float)(o_y+r*Math.sin(endangel*Math.PI/180))); path.addArc(rect, startangel, endangel-startangel); canvas.clipPath(path); canvas.drawCircle(o_x, o_y, r, paint); } public void Draw(Canvas canvas){ Log.d("2","Button的Draw(canvas)."); switch(status){ case 0: if(rotate<=1.1f){ getArc(canvas,x,y,r,270,270+360*rotate,paint); rotate += 0.02f; } else{ status = 1; Draw(canvas); } break; case 1: paint.setColor(Color.parseColor(color)); canvas.drawCircle(x, y, r, paint); paint.setColor(Color.WHITE); canvas.drawText(data, x - strwid/2, y+strhei/2, paint); break; } } public boolean onClick(float x,float y){ return ((x-this.x)*(x-this.x) + (y-this.y) * (y-this.y))<=r*r; } /* private class Drawthread extends Thread{ Canvas canvas; private Drawthread(Canvas canvas){ this.canvas = canvas; } public void run(){ while(rotate<=1.1f){ // Draw(canvas); rotate += 0.05f; try{ Thread.sleep(timepan); } catch(Exception e){ e.printStackTrace(); } } status = 1; Draw(canvas); // Button.this.postInvalidate(); } }*/ }
TextSurfaceView.java
package com.example.button; import android.annotation.SuppressLint; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.util.Log; import android.view.MotionEvent; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.Toast; public class TextSurfaceView extends SurfaceView implements SurfaceHolder.Callback{ MyActivity activity; Button[] b = new Button[10]; int num = 1; int status = 0; KeyThread kt; Paint paint; float pointx; float pointy; String[] color = {"#FF0000","#00e9ff","#ffff00","#e500ff","#0008ff", "#00ff34","#ff9d00","#000000","#cccccc","#0084cc"}; String[] data = {"open","hello","你好","報表查詢","reset", "start","begin","資訊查詢","good","day"}; public TextSurfaceView(MyActivity activity) { super(activity); this.activity = activity; this.getHolder().addCallback(this); } @Override public void onDraw(Canvas canvas){ Log.d("6","呼叫onDraw(canvas)."); super.onDraw(canvas); canvas.drawColor(Color.WHITE); int i; for(i=-1;++i<num;){ b[i].Draw(canvas); } } @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder arg0) { // Log.d("4","TextSurfaceView的KeyThread.start()"); b[0] = new Button(160,100,40,"open","#00e9ff"); paint = new Paint(); paint.setColor(Color.WHITE); kt = new KeyThread(); kt.setKeyFlag(true); kt.start(); } @SuppressLint("ClickableViewAccessibility") @Override public boolean onTouchEvent(MotionEvent event){ pointx = event.getX(); pointy = event.getY(); switch(event.getAction()){ case MotionEvent.ACTION_DOWN://按下 break; case MotionEvent.ACTION_MOVE://移動 break; case MotionEvent.ACTION_UP://擡起 int i = -1; for(;++i<num;){ if(b[i].onClick(pointx, pointy)){ Toast.makeText(activity, b[i].data, Toast.LENGTH_SHORT).show(); return true; } } if(num<10){ b[num++] = new Button(pointx,pointy,40,data[(int)(Math.random()*10)],color[(int)(Math.random()*10)]); } break; default: break; } return true; } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub kt.setKeyFlag(false); } private class KeyThread extends Thread{ private boolean keyFlag = false; @Override public void run(){ while(isKeyFlag()){ try{ Thread.sleep(20); } catch(Exception e){ e.printStackTrace(); } repaint(); } } public boolean isKeyFlag(){ return keyFlag; } public void setKeyFlag(boolean keyFlag){ this.keyFlag = keyFlag; } } @SuppressLint("WrongCall") public void repaint() { Canvas canvas = this.getHolder().lockCanvas(); try{ synchronized(canvas){ onDraw(canvas); } } catch(Exception e){ e.printStackTrace(); } finally{ if(canvas!=null){ this.getHolder().unlockCanvasAndPost(canvas); } } } }
MyActivity.java
package com.example.button; import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.util.Log; public class MyActivity extends Activity{ TextSurfaceView tsv; @Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); tsv = new TextSurfaceView(this); Log.d("5","MyActivity中的setContentView(tsv)"); setContentView(tsv); DisplayMetrics dm = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(dm); if(dm.widthPixels > dm.heightPixels){ Constant.SCREEN_WIDTH = dm.heightPixels; Constant.SCREEN_HEIGHT = dm.widthPixels; } else{ Constant.SCREEN_WIDTH = dm.widthPixels; Constant.SCREEN_HEIGHT = dm.heightPixels; } } }
Constant.java
package com.example.button;
public class Constant {
public static int SCREEN_HEIGHT;
public static int SCREEN_WIDTH;
}