点击之后View中的内容滑动到指定位置
例1:
利用
Scroller类
scrollTo(x,y);view中的内容移动到的坐标点
scrollBy(x,y);View中的内容滑动距离;
并且坐标系与一般坐标系方向相反
package com.moredo.ui;
import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Path.Direction;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;
public class ScrollerViewTest extends View
{
private Paint paint;
private Path path;
private Context mContext;
private Scroller mScroller;
public ScrollerViewTest(Context context)
{
super(context);
initView(context);
}
public ScrollerViewTest(Context context, AttributeSet attrs)
{
super(context, attrs);
initView(context);
}
public ScrollerViewTest(Context context, AttributeSet attrs, int defStyleAttr)
{
super(context, attrs, defStyleAttr);
initView(context);
}
private void initView(Context context)
{
mContext = context;
mScroller = new Scroller(mContext);
paint = new Paint();
paint.setAntiAlias(true);
paint.setStyle(Paint.Style.STROKE);
paint.setColor(Color.BLACK);
paint.setTextSize(30);
path = new Path();
path.addCircle(0, 0, 50, Direction.CW);
}
@Override
protected void onDraw(Canvas canvas)
{
canvas.drawTextOnPath("一去二三里,烟村四五家", path, 0, 0, paint);
}
/**
* 滑动到点击位置
* @param destX
* @param destY
*/
private void smoothScrollerTo(int destX, int destY)
{
///< 获取所在位置
int scrollX = (int) getX();
int scrollY = (int) getY();
///< 获取现在滑动到的位置
getScrollX();
///< 获取距离之差
int deltaX = scrollX - destX;
int deltaY = scrollY - destY;
mScroller.startScroll(scrollX, scrollY, deltaX, deltaY, 1000);
invalidate();
}
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
///< 获取到相对于屏幕的位置
int x = (int) event.getRawX();
int y = (int) event.getRawY();
smoothScrollerTo(x, y);
}
return true;
}
@Override
public void computeScroll()
{
if(mScroller.computeScrollOffset())
{
///< 移动到这个点
scrollTo(mScroller.getCurrX(), mScroller.getCurrY());
}
postInvalidate();
}
}
利用动画
代码示例
package com.moredo.activity;
import android.animation.ValueAnimator;
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import com.moredo.velocitytracker.R;
/**
* 作者:Administrator on 2016/9/7 22:53
*/
public class ScrollerActivity extends Activity
{
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.x_layout);
mButton = (Button) findViewById(R.id.but);
///< 开始滑动的位置
final int startX = 0,startY = 0;
///< 滑动的距离
final int deltaX = -200,deltaY = -200;
///< 属性动画
ValueAnimator animator = ValueAnimator.ofInt(0,1).setDuration(10000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
///< 获取动画百分比
float fraction = animation.getAnimatedFraction();
///< 控制按钮内容的滑动
mButton.scrollTo((int) (deltaX * fraction),(int)(deltaY * fraction));
}
});
///< 开始动画
animator.start();
}
}