在使用GestureDetector实现Scroller后,如何更新Android ShapedRavable边界更改?
我尝试过的: 我正在用指定的边界在在使用GestureDetector实现Scroller后,如何更新Android ShapedRavable边界更改?,android,android-canvas,android-gesture,android-shape,Android,Android Canvas,Android Gesture,Android Shape,我尝试过的: 我正在用指定的边界在onDraw(画布)中绘制shapedrawable 添加了允许视图向任何方向滚动的视图,如本SO中提到的2d滚动视图 问题: 当我滚动时,onScroll()的视图GestureDetector shapeDrawable的绑定会发生变化!是否有任何方法可以在滚动时更改/更新已显示的shapeDrawable边界 我已经实现了onTouchEvent(MotionEvent事件)这样,如果我单击画布/视图上显示的Shape drawable,如果单击的x、y包
onDraw(画布)
中绘制shapedrawable
添加了允许视图向任何方向滚动的视图,如本SO中提到的2d滚动视图
问题:
当我滚动时,onScroll()的视图GestureDetector shapeDrawable的绑定会发生变化!是否有任何方法可以在滚动时更改/更新已显示的shapeDrawable边界
我已经实现了onTouchEvent(MotionEvent事件)
这样,如果我单击画布/视图上显示的Shape drawable,如果单击的x、y包含shapedrawable的边界,则删除该drawable。当视图未滚动时,如果由于边界和x、y参数的更改,滚动视图边界无法检测到shapedrawable,则此功能可以正常工作
代码:
导入android.content.Context;
导入android.graphics.Canvas;
导入android.graphics.Color;
导入android.graphics.Rect;
导入android.graphics.drawable.ShapeDrawable;
导入android.graphics.drawable.shapes.OvalShape;
导入android.graphics.drawable.shapes.RectShape;
导入android.graphics.drawable.shapes.Shape;
导入android.os.Build;
导入android.util.AttributeSet;
导入android.view.GestureDetector;
导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.Scroller;
导入java.util.*;
公共类ShapeDrawableView扩展了视图{
私有列表形状=新的ArrayList();
私有整数[]mColors={Color.BLACK,Color.BLUE,Color.GREEN,
Color.RED};
//如果以编程方式创建并使用setContentView添加
公共ShapeDrawableView(上下文){
超级(上下文);
}
私人卷轴;
私人手势检测器;
公共ShapeDrawableView(上下文、属性集属性){
超级(上下文,attrs);
mDetector=new GestureDetector(shapedrawbleview.this.getContext(),
新的手势监听器());
if(Build.VERSION.SDK_INT<11){
mScroller=新滚动条(getContext());
}否则{
mScroller=new Scroller(getContext(),null,true);
}
}
@凌驾
受保护的void onDraw(画布){
super.onDraw(帆布);
用于(形状可移动形状:形状){
形状。绘制(画布);
}
}
@凌驾
公共布尔onTouchEvent(运动事件){
mDetector.onTouchEvent(事件);
if(event.getAction()==MotionEvent.ACTION\u向下){
int x=(int)event.getX();//将getX(int)用于多指
//手势
int y=(int)event.getY();
如果(!IsDeleteingExistingShape(x,y)){
添加(makeshapedravable(x,y));
}
使无效();
返回(true);//已处理的触摸事件
}否则{
return(false);//未处理触摸事件
}
}
私有布尔isDeletingExistingShape(整数x,整数y){
用于(形状可移动形状:形状){
Rect bounds=shape.getBounds();
if(边界包含(x,y)){
形状。移除(形状);
返回(真);
}
}
返回(假);
}
私有可变形可变形可变形(intx,inty){
int maxWidth=getWidth()/10;
int maxHeight=getHeight()/10;
形状;
if(Math.random()<0.5){
形状=新的椭圆形();
}否则{
形状=新矩形形状();
}
ShapedRavable shapeD=新的ShapedRavable(形状);
int width=RandomUtils.randomInt(maxWidth)+5;
int-height=RandomUtils.randomInt(最大高度)+5;
成形立根(x-宽度/2,y-高度/2,x+宽度/2,y
+高度(2米),;
shapeD.getPaint().setColor(RandomUtils.randomElement(mColors));
返回(成形);
}
私有类GestureListener扩展
GestureDetector.SimpleOnGestureListener{
@凌驾
公共布尔onScroll(运动事件e1、运动事件e2、,
浮动距离X,浮动距离Y){
//直接设置饼图旋转。
//浮动滚动θ=矢量滚动或滚动(
//距离,
//距离,
//e2.getX()-mPieBounds.centerX(),
//e2.getY()-mPieBounds.centerY();
//setPieRotation(getPieRotation()-(int)scrollTheta/
//速度下降);
scrollBy((int)distanceX,(int)distanceY);
返回true;
}
@凌驾
公共布尔onFling(运动事件e1、运动事件e2、浮点速度X、,
浮动速度y){
//设置滚动条以供玩乐
/*
*浮动滚动θ=矢量浮动滚动圈(速度x,速度y,
*e2.getX()-mPieBounds.centerX(),e2.getY()-
*mPieBounds.centerY();mScroller.fling(0,(int)
*getPieRotation(),0,(int)scrollTheta/
*FLING_VELOCITY_DOWNSCALE,0,0,Integer.MIN_值,
*整数(最大值);
*
*//启动动画师并告诉它为预期的动画制作动画
*投掷的持续时间.if(Build.VERSION.SDK_INT>=11){
*mscroll animator.setDuration(mscroll.getDuration());
*mScrollAnimator.start();}
*/
mScroller.fling(getScrollX(),getScrollY(),-(int)velocityX,
-(int)速度y,0,(int)100,0,(int)100;
invalidate();//不记得是否需要它
返回true;
//返回true;
}
@凌驾
公共布尔onDown(运动事件e)
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Rect;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.OvalShape;
import android.graphics.drawable.shapes.RectShape;
import android.graphics.drawable.shapes.Shape;
import android.os.Build;
import android.util.AttributeSet;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Scroller;
import java.util.*;
public class ShapeDrawableView extends View {
private List<ShapeDrawable> shapes = new ArrayList<ShapeDrawable>();
private Integer[] mColors = { Color.BLACK, Color.BLUE, Color.GREEN,
Color.RED };
// If made programmatically and added with setContentView
public ShapeDrawableView(Context context) {
super(context);
}
private Scroller mScroller;
private GestureDetector mDetector;
public ShapeDrawableView(Context context, AttributeSet attrs) {
super(context, attrs);
mDetector = new GestureDetector(ShapeDrawableView.this.getContext(),
new GestureListener());
if (Build.VERSION.SDK_INT < 11) {
mScroller = new Scroller(getContext());
} else {
mScroller = new Scroller(getContext(), null, true);
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (ShapeDrawable shape : shapes) {
shape.draw(canvas);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
mDetector.onTouchEvent(event);
if (event.getAction() == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX(); // Use getX(int) for multi-finger
// gestures
int y = (int) event.getY();
if (!isDeletingExistingShape(x, y)) {
shapes.add(makeShapeDrawable(x, y));
}
invalidate();
return (true); // Handled touch event
} else {
return (false); // Did not handle touch event
}
}
private boolean isDeletingExistingShape(int x, int y) {
for (ShapeDrawable shape : shapes) {
Rect bounds = shape.getBounds();
if (bounds.contains(x, y)) {
shapes.remove(shape);
return (true);
}
}
return (false);
}
private ShapeDrawable makeShapeDrawable(int x, int y) {
int maxWidth = getWidth() / 10;
int maxHeight = getHeight() / 10;
Shape shape;
if (Math.random() < 0.5) {
shape = new OvalShape();
} else {
shape = new RectShape();
}
ShapeDrawable shapeD = new ShapeDrawable(shape);
int width = RandomUtils.randomInt(maxWidth) + 5;
int height = RandomUtils.randomInt(maxHeight) + 5;
shapeD.setBounds(x - width / 2, y - height / 2, x + width / 2, y
+ height / 2);
shapeD.getPaint().setColor(RandomUtils.randomElement(mColors));
return (shapeD);
}
private class GestureListener extends
GestureDetector.SimpleOnGestureListener {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2,
float distanceX, float distanceY) {
// Set the pie rotation directly.
// float scrollTheta = vectorToScalarScroll(
// distanceX,
// distanceY,
// e2.getX() - mPieBounds.centerX(),
// e2.getY() - mPieBounds.centerY());
// setPieRotation(getPieRotation() - (int) scrollTheta /
// FLING_VELOCITY_DOWNSCALE);
scrollBy((int) distanceX, (int) distanceY);
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// Set up the Scroller for a fling
/*
* float scrollTheta = vectorToScalarScroll( velocityX, velocityY,
* e2.getX() - mPieBounds.centerX(), e2.getY() -
* mPieBounds.centerY()); mScroller.fling( 0, (int)
* getPieRotation(), 0, (int) scrollTheta /
* FLING_VELOCITY_DOWNSCALE, 0, 0, Integer.MIN_VALUE,
* Integer.MAX_VALUE);
*
* // Start the animator and tell it to animate for the expected
* duration of the fling. if (Build.VERSION.SDK_INT >= 11) {
* mScrollAnimator.setDuration(mScroller.getDuration());
* mScrollAnimator.start(); }
*/
mScroller.fling(getScrollX(), getScrollY(), -(int) velocityX,
-(int) velocityY, 0, (int) 100, 0, (int) 100);
invalidate(); // don't remember if it's needed
return true;
// return true;
}
@Override
public boolean onDown(MotionEvent e) {
// The user is interacting with the pie, so we want to turn on
// acceleration
// so that the interaction is smooth.
/*
* mPieView.accelerate(); if (isAnimationRunning()) {
* stopScrolling(); }
*/
if (!mScroller.isFinished()) { // is flinging
mScroller.forceFinished(true); // to stop flinging on touch
}
return true; // else won't work
// return true;
}
}
}
x += getScrollX();
y += getScrollY();