package com.lixinyang.myviews.Views; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.support.annotation.Nullable; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; import com.lixinyang.myviews.R; /** * author:Created by WangZhiQiang on 2017/12/27. */ public class MyImage extends View { private Bitmap background; private Bitmap slide; private boolean state; private boolean isTouch; private aa aa_dui; public MyImage(Context context) { this(context,null); } public MyImage(Context context, @Nullable AttributeSet attrs) { this(context, attrs,0); } public MyImage(Context context, @Nullable AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); background = BitmapFactory.decodeResource(getResources(), R.drawable.background); slide = BitmapFactory.decodeResource(getResources(), R.drawable.slide); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawBitmap(background,0,0,null); int left=0; if(isTouch){ //正常情况下应该是左边移到了你点的位置除去恩扭的一半就变成恩扭的中间到你手点的地方了 left=currentX-slide.getWidth()/2; if(left<0){ left=0; } if(left>background.getWidth()-slide.getWidth()){ left=background.getWidth()-slide.getWidth(); } canvas.drawBitmap(slide,left,0,null); }else{ if(state){ left=background.getWidth()-slide.getWidth(); }else{ left=0; } canvas.drawBitmap(slide,left,0,null); } } private int currentX; @Override public boolean onTouchEvent(MotionEvent event) { switch(event.getAction()){ case MotionEvent.ACTION_DOWN: isTouch=true; currentX = (int) event.getX(); break; case MotionEvent.ACTION_MOVE: currentX = (int) event.getX(); break; case MotionEvent.ACTION_UP: isTouch=false; currentX = (int) event.getX(); int contentX = background.getWidth()/2; if(currentX>contentX){ state=true; }else{ state=false; } aa_dui.haha(state); break; } //重新绘制一下 invalidate(); return true; } @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); setMeasuredDimension(background.getWidth(),background.getHeight()); } public interface aa{ void haha(boolean state); } //提供一个来让外部访问的方法 public void bb(aa aa_dui){ this.aa_dui = aa_dui; } }
MainActivity
package com.lixinyang.myviews; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.Window; import android.widget.Toast; import com.lixinyang.myviews.Views.MyImage; public class MainActivity extends AppCompatActivity { private static Window window; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //window = getWindow(); MyImage mi = findViewById(R.id.myimage); mi.bb(new MyImage.aa() { @Override public void haha(boolean state) { Toast.makeText(MainActivity.this,"当前状态是"+state,Toast.LENGTH_LONG).show(); } }); } /*public static int aa(){ Rect frame = new Rect(); window.getDecorView().getWindowVisibleDisplayFrame(frame); int statusBarHeight = frame.top; int contentTop = window.findViewById(Window.ID_ANDROID_CONTENT).getTop(); //statusBarHeight是上面状态栏的高度 int titleBarHeight = contentTop - statusBarHeight; //Toast.makeText(MainActivity.this,""+titleBarHeight,Toast.LENGTH_LONG).show(); return titleBarHeight; }*/ }