編輯:關於Android編程
MainActivity如下:<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4KPHByZSBjbGFzcz0="brush:java;">package cc.testview3; import cc.testview3.SwitchView.SwitchChangedListener; import android.os.Bundle; import android.widget.Toast; import android.app.Activity; /** * Demo描述: * 自定義View實現滑動開關 * * 測試設備: * 分辨率為480x854 */ public class MainActivity extends Activity { private SwitchView mSwitchView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ mSwitchView=(SwitchView) findViewById(R.id.switchView); mSwitchView.initSwitchStatus(true); mSwitchView.setOnSwitchChangedListener(new SwitchChangedListenerImpl()); } private class SwitchChangedListenerImpl implements SwitchChangedListener{ @Override public void OnChanged(boolean currentStatus) { Toast.makeText(MainActivity.this, "currentIsOff?-->"+currentStatus, Toast.LENGTH_SHORT).show(); } } }
SwitchView如下:
package cc.testview3; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; public class SwitchView extends View { private Bitmap mOnBitmap; private Bitmap mOffBitmap; private Bitmap mDotBitmap; private float currentX; private boolean currentIsSlipping=false; private boolean currentIsOff; private SwitchChangedListener mSwitchChangedListener; private int dotWidth; private int switchWidth; public SwitchView(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); initSwitchView(); } public SwitchView(Context context, AttributeSet attrs) { super(context, attrs); initSwitchView(); } public SwitchView(Context context) { super(context); initSwitchView(); } private void initSwitchView(){ mOnBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.on); mOffBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.off); mDotBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.dot); dotWidth=mDotBitmap.getWidth(); switchWidth=mOnBitmap.getWidth(); this.setOnTouchListener(new TouchListenerImpl()); } public void initSwitchStatus(boolean isOff){ if (isOff) { currentX=switchWidth; } else { currentX=0; } currentIsOff=isOff; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); } @Override protected void onLayout(boolean changed, int left, int top, int right,int bottom) { super.onLayout(changed, left, top, right, bottom); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); Matrix matrix=new Matrix(); Paint paint=new Paint(); //先畫出開關的背景(關/開) if (currentIsOff) { canvas.drawBitmap(mOffBitmap, matrix, paint); } else { canvas.drawBitmap(mOnBitmap, matrix, paint); } //再畫出滑塊 //1 在滑動中(if),滑塊的left就是不斷變化的currentX //2 手指抬起後(else)停止滑動時.此時的開關應該在左右 // 其中一側處於開或者關的狀態 if (currentIsSlipping) { canvas.drawBitmap(mDotBitmap, currentX, 17, paint); } else { if (currentIsOff) { canvas.drawBitmap(mDotBitmap, currentX-dotWidth, 17, paint); }else{ canvas.drawBitmap(mDotBitmap, currentX, 17, paint); } } } private class TouchListenerImpl implements OnTouchListener{ @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_MOVE: currentIsSlipping=true; currentX = event.getX(); if (currentX>switchWidth/2) { currentIsOff=true; }else { currentIsOff=false; } //防止向右邊滑動時越界 if (event.getX()>switchWidth-dotWidth) { currentX = switchWidth-dotWidth; currentIsOff=true; } //防止向左邊滑動時越界 if (event.getX()<0) { currentX=0; currentIsOff=false; } //重繪!!! invalidate(); break; case MotionEvent.ACTION_UP: currentIsSlipping=false; currentX = event.getX(); //抬起時若(if)已經超過開關一般的長度,則讓其處於關閉的狀態 //否則(else)讓其處於打開的狀態 if (currentX >= switchWidth / 2) { currentX = switchWidth; currentIsOff=true; } else { currentX = 0; currentIsOff=false; } if (mSwitchChangedListener != null) { mSwitchChangedListener.OnChanged(currentIsOff); } // 重繪!!! invalidate(); break; default: break; } return true; } } // 接口 public interface SwitchChangedListener { public void OnChanged(boolean currentIsOff); } public void setOnSwitchChangedListener(SwitchChangedListener switchChangedListener) { this.mSwitchChangedListener = switchChangedListener; } }
main.xml如下:
vivo x7怎麼截圖?vivo x7手機是剛剛發布出來的新機,可能有用戶還不會截屏,下文介紹vivo x7截屏圖文流程,一起來瞧瞧吧! vivo x7截
一.前言本來覺得so easy,真是沒想到,還搞了老半天,搞的我大汗淋漓,要拍桌子摔鍵盤了。本想實現的功能是:通過網易雲音樂/百度音樂/QQ音樂/酷狗音樂中一個API,通
Toast大家都很熟,不多說。直接上圖上代碼。 具體代碼如下:main.xm
1.RxJava概述ReactiveX與RxJava在講到RxJava之前我們首先要了解什麼是ReactiveX,因為RxJava是ReactiveX的一種java實現。