我正在制作Android应用程序而且我有一件棘手的事情要做.我需要在画布上绘制一条路径,但绘图应该是动画的(即,稍微延迟后逐点绘制).
是否可以使用Android SDK制作这样的东西?如果没有,我怎么能产生这种效果?
我正在尝试在ImageView中创建无限脉冲效果.但是如何保持偏移呢?
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<scale
android:duration="700"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="0.5"
android:toYScale="0.5"/>
<scale
android:duration="700"
android:fromXScale="1"
android:fromYScale="1"
android:pivotX="50%"
android:pivotY="50%"
android:startOffset="700"
android:toXScale="2"
android:toYScale="2"/>
</set>
Run Code Online (Sandbox Code Playgroud) 我创建了一个扩展的类,View我将在一个布局中引用它,以便在屏幕上绘制.
这个类只是表示一个Rectangle,我希望矩形的长度一直减小到0.
构造函数:
public CardAnimationNew(Context context, AttributeSet attrs) {
super(context, attrs);
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setColor(getResources().getColor(R.color.card_grey_underline));
}
Run Code Online (Sandbox Code Playgroud)
onMeasure:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
if (mRectangle == null) {
mWidth = getMeasuredWidth();
mHeight = getMeasuredHeight();
mRectangle = new Rect(0, 0, mWidth, mHeight);
animateRectangle();
}
}
Run Code Online (Sandbox Code Playgroud)
animateRectangle:
private void animateRectangle() {
mObjectAnimator = ObjectAnimator.ofFloat(mRectangle, "width", 5);
mObjectAnimator.setDuration(1000);
mObjectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator valueAnimator) {
invalidate();
}
});
mObjectAnimator.start();
}
Run Code Online (Sandbox Code Playgroud)
onDraw有:
@Override
protected void …Run Code Online (Sandbox Code Playgroud) 使用我在StackOverflow 答案中找到的代码,我可以成功地用手指在 Canvas 中绘制任何内容,并且在绘制时我会看到我绘制的内容。由此,我想创建一个在按下按钮时触发的函数,该函数将执行两件事:
为此,我稍微修改了代码onTouchEvent,因此它相应地存储每个绘制点:
@Override
public boolean onTouchEvent(MotionEvent event) {
StrokePoint point;
switch (event.getAction()){
case MotionEvent.ACTION_DOWN:
mPath.moveTo(event.getX(), event.getY());
// Retrieve strokes in memory
stroke_buffer = new Stroke();
stroke_buffer.points = new ArrayList<StrokePoint>();
point = new StrokePoint();
point.x = event.getX();
point.y = event.getY();
stroke_buffer.points.add( point );
break;
case MotionEvent.ACTION_MOVE:
mPath.lineTo(event.getX(), event.getY());
// Retrieve strokes in memory
point = new StrokePoint();
point.x = event.getX();
point.y = event.getY();
stroke_buffer.points.add( point );
invalidate();
break;
case MotionEvent.ACTION_UP:
// Retrieve strokes …Run Code Online (Sandbox Code Playgroud) 我需要捕获标记才能在Android的画布上绘制图形,其效果类似于以下gif:
好了,到目前为止,我可以通过ValueAnimator绘制具有恒定速度的边。但是,我一次只能绘制一侧,因为绘制另一侧时无法保存最后一侧。那么,有没有解决问题的好方法?
通过ValueAnimator缓慢画线的代码?
GraphicsView.java
public class GraphicsView extends View {
private int stepX, stepY = 0;
private int startX, startY, stopX, stopY = 0;
private Paint paint = null;
public GraphicsView(Context context) {
super(context);
// Paint
paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.RED);
paint.setStyle(Paint.Style.STROKE);
startX = 40;
startY = 397;
stopX = 1040;
stopY = 397;
Init();
}
public void Init(){
ValueAnimator animatorX = ValueAnimator.ofFloat(startX, stopX);
ValueAnimator animatorY = ValueAnimator.ofFloat(startY, stopY);
animatorX.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我正在制作一个需要动态动画的应用程序。(玩家动作)我正在使用该Canvas对象来执行此操作。我的第一个问题是“Canvas真的是处理这些动画的最佳方法吗?”,我的第二个问题是“如何将玩家重新绘制到Canvas?” 这是我的代码:
theGame.java:
package birdprograms.freezetag;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
public class theGame extends Activity {
players[] arr = {
new player(),
new player(),
new player(),
new player()
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new myView(this));
}
public class myView extends View {
Paint paint = new Paint();
public myView(Context context) {
super(context);
paint.setColor(Color.YELLOW);
}
@Override
public void onDraw(final Canvas canvas) {
arr[0].update(true, …Run Code Online (Sandbox Code Playgroud) 我有一个图像视图"石头",并将其从当前位置移动到X,Y位置.我希望它沿着曲线移动.请告诉我如何做到这一点(我已将min api设为11)
ObjectAnimator moveX = ObjectAnimator.ofFloat(stone, "x", catPos[0] );
ObjectAnimator moveY = ObjectAnimator.ofFloat(stone, "y", catPos[1] );
AnimatorSet as = new AnimatorSet();
as.playTogether(moveX, moveY);
as.start();
Run Code Online (Sandbox Code Playgroud)