編輯:關於Android編程
因項目緣故需重新定制SwitchButton,效果如下:
過程如下:
1.圓角矩形的繪制
2.字體繪制
3.小圓繪制
4.左右滑動動畫效果繪制
代碼如下:
package com.smart.view; import java.util.Timer; import java.util.TimerTask; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.FontMetrics; import android.graphics.Rect; import android.graphics.RectF; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class MySwitchButton extends View{ private int w, h;//組件寬高 private boolean flag_switch;//開關標志 private boolean flag_touch;//觸摸標志 private String ON = "ON"; private String OFF = "OFF"; private float textSize = 40;//字體大小 private int color1 = 0xff999999;//關背景顏色 private int color2 = 0xff3e9edb;//開背景顏色 private int color_bg = color1; private int circle_bg_nomal = Color.WHITE;//圓正常情況下的背景 private int circle_bg_touch = Color.YELLOW;//觸摸時圓的背景 //圓心 private float y; private float x; private float radius;//圓半徑 private int distance = 10;//左右邊距 private OnCircleClickListner mOnCircleClickListner; public MySwitchButton(Context context) { super(context); System.out.println("---MySwitchButton1---"); } public MySwitchButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); System.out.println("---MySwitchButton2---"); } public MySwitchButton(Context context, AttributeSet attrs) { super(context, attrs); System.out.println("---MySwitchButton3---"); } public void setOnCircleClickListner(OnCircleClickListner mOnCircleClickListner){ this.mOnCircleClickListner = mOnCircleClickListner; } public boolean isFlag_switch() { return flag_switch; } public void setChecked(boolean flag_switch) { this.flag_switch = flag_switch; if(flag_switch){ x = distance+radius; color_bg = color2; }else{ x = w-(distance+radius); color_bg = color1; } circleMove(); // invalidate(); } @Override public boolean onTouchEvent(MotionEvent event) { // float x = event.getX(); // float y = event.getY(); switch (event.getAction()) { case MotionEvent.ACTION_DOWN: flag_touch = true; invalidate(); break; case MotionEvent.ACTION_MOVE: break; case MotionEvent.ACTION_UP: if(mOnCircleClickListner!=null){ mOnCircleClickListner.onClick(MySwitchButton.this,!flag_switch); } flag_touch = false; invalidate(); break; } return true; } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); this.w = w; this.h = h; y = h/2; radius = h/2-distance; x = distance+radius; // color_bg = color1; circleMove(); // invalidate(); System.out.println("-----onSizeChanged----"); // System.out.println("x:"+x); // System.out.println("w:"+w); // System.out.println("h:"+h); } @Override protected void onDraw(Canvas canvas) { System.out.println("-----onDraw----"); Paint paint = new Paint(); paint.setAntiAlias(true); canvas2RoundRect(canvas,paint);//圓角矩形 canvas2Text(canvas,paint);//字體 canvas2Circle(canvas,paint);//小圓 super.onDraw(canvas); } public void canvas2RoundRect(Canvas canvas,Paint paint){ paint.setColor(color_bg); RectF oval3 = new RectF(0, 0, w, h); canvas.drawRoundRect(oval3, 45, 45, paint); } public void canvas2Circle(Canvas canvas,Paint paint){ if(flag_touch){ paint.setColor(circle_bg_touch); }else{ paint.setColor(circle_bg_nomal); } // circleMove(); // System.out.println("drawX:"+x); canvas.drawCircle(x, y, radius, paint); } Timer timer; TimerTask task; public void circleMove(){//小圓運動軌跡 final float startX =distance+radius; final float endX = w-(distance+radius); final long upDataDistance = 10; if(timer!=null){ task.cancel(); timer.cancel(); timer=null; task=null; } timer = new Timer(); task = new TimerTask() { @Override public void run() { if(flag_switch){ x+=upDataDistance;//打開 }else{ x-=upDataDistance;//關閉 } if(x>=endX){ x=endX; this.cancel(); timer.cancel(); timer =null; }else if(x
監聽接口時要調用public void setChecked(boolean flag_switch)該方法才能使小球左右滑動
本例主要實現在APP中解析GML數據並顯示GML,地理標記語言(外語全稱:Geography MarkupLanguage、外語縮寫:GML),它由開放式地理信息系統協會
我將模擬器解壓到C:\android-sdk-windows\在C:\android-sdk-windows\platform-tools這個文件夾裡有個a
Android的消息機制幾乎是面試必問的話題,當然也並不是因為面試,而去學習,更重要的是它在Android的開發中是必不可少的,占著舉足輕重的地位,所以弄懂它是很有必要的
引言:去年Android 6.0發布後,其新引入的(Requesting Permissions at Run Time)運行時權限就備受開發者關注,隨著今年國內手機廠商