android滑动动画效果,Android滑块上下移动的实现(加速动画
效果)
1、MainActivity
package com.xpg.slider;
import com.xpg.view.SliderView;
import com.xpg.view.SliderView.SliderListener;
import android.os.Bundle;
import android.app.Activity;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
public class MainActivity extends Activity implements SliderListener{
private RelativeLayout relativeLayout ;
@Override
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
//初始化SliderView
relativeLayout = (RelativeLayout)findViewById(R.id.slider);
SliderView sliderView = new SliderView(this,this);
RelativeLayout.LayoutParams sliderLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
sliderLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
sliderView.setLayoutParams(sliderLayoutParams);
relativeLayout.addView(sliderView);
sliderView.setSilderListener(this);
}
@Override
public void sliderIsUp() {
// TODO Auto-generated method stub
System.out.println("实现接⼝");
}
@Override
public void sliderIsDown() {
/
/ TODO Auto-generated method stub
System.out.println("返回原点");
}
}
2、SliderView
package com.xpg.view;
import com.xpg.slider.R;
import android.R.integer;
import android.app.Activity;
t.Context;
aphics.Bitmap;
aphics.BitmapFactory;
aphics.Canvas;
aphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class SliderView extends View {
private Context context;
private Bitmap slipopenBtn, slipopenBg;canvas动画
private float y, x, drawY,progress;//progress
public SliderListener silderListener ;
private int height,bottomMargin;//加⼊leftMargin变量是为了让滑块 离两边有个距离private Handler myHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
drawY = msg.arg1;
invalidate();
super.handleMessage(msg);
}
@Override
public void dispatchMessage(Message msg) {
// TODO Auto-generated method stub
super.dispatchMessage(msg);
}
};
public SliderView(Context context) {
super(context);
init();
}
public SliderView(Context context , SliderListener openlocklistener) {
super(context);
this.silderListener = openlocklistener;
init();
}
public SliderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public SliderView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
private void init() {
slipopenBtn = BitmapFactory.decodeResource(getResources(),
R.drawable.hbs_home_open_tab_bg2);
slipopenBg = BitmapFactory.decodeResource(getResources(),
R.drawable.hbs_home_open_tab_bg);
height = ((Activity)context).getWindowManager().getDefaultDisplay().getHeight(); //bottomMargin = und(37f/480f*height);//
bottomMargin = Height()/Height();
drawY = Height()/2 -bottomMargin;//初始化drawY的初始值progress = Height()/2 -bottomMargin;//初始化progress的值}
@Override
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
canvas.drawBitmap(slipopenBg, 0, 0, paint);
//canvas.drawBitmap(slipopenBtn, drawY, Height() / 2
//- Height() / 2, paint);
canvas.drawBitmap(slipopenBtn, 0, drawY , paint);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int i = 0xff & Action();
switch (i) {
case 0: //
break;
case 5: //
break;
case 2: // 按住
this.y = Y();
this.x = X();
/
/ 按住的限制条件
if (x <= Width()) {
if ((y <= Height() - Height() / 2)
&& (drawY >= bottomMargin + bottomMargin / 2)
&& (y >= bottomMargin + Height() / 4)) {
drawY = y - Height() / 2;
}
break;
}
case 1: // 松开
if (drawY <= bottomMargin + bottomMargin/2) {
drop();
silderListener.sliderIsUp();
}
if (drawY > bottomMargin + bottomMargin / 2) {
drawY = Height() / 2 - bottomMargin; // 没开锁,滑块重新回到初始位置}
break;
case 6:
break;
default:
break;
}
invalidate();
return true;
}
/**
* 启动线程
*/
private void drop(){
new Thread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
motion();
}
}).start();
}
/**
* 设置动作效果
*/
private void motion(){
for (int i = 0; i
if (i==0) {//停留⼀段时间
try {
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论