我将 kenburnsView 添加到我的项目中,我在主要活动中需要它。
我从以下来源获得了 kenBurnsView 代码:KenBurnsView.java
问题:
有趣的是,代码在新旧设备上都能完美运行,但不适用于运行 Android Kitkat(特别是手机)的设备。
我尝试在以下代码上实现一些线程,但仍然有大量访问 Main ui 线程。
这是代码:
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewPropertyAnimator;
import android.widget.FrameLayout;
import android.widget.ImageView;
import java.util.Random;
/**
* Created by f.laurent on 21/11/13.
*/
public class KenBurnsView extends FrameLayout {
private static final String TAG = "KenBurnsView";
private final Handler mHandler;
private int[] mResourceIds;
private ImageView[] mImageViews;
private int mActiveImageIndex = -1;
private final Random random = new Random();
private int mSwapMs = 10000;
private int mFadeInOutMs = 400;
private float maxScaleFactor = 1.5F;
private float minScaleFactor = 1.2F;
private Runnable mSwapImageRunnable = new Runnable() {
@Override
public void run() {
swapImage();
mHandler.postDelayed(mSwapImageRunnable, mSwapMs - mFadeInOutMs*2);
}
};
public KenBurnsView(Context context) {
this(context, null);
}
public KenBurnsView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public KenBurnsView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mHandler = new Handler();
}
public void setResourceIds(int... resourceIds) {
mResourceIds = resourceIds;
fillImageViews();
}
private void swapImage() {
Log.d(TAG, "swapImage active=" + mActiveImageIndex);
if(mActiveImageIndex == -1) {
mActiveImageIndex = 1;
animate(mImageViews[mActiveImageIndex]);
return;
}
int inactiveIndex = mActiveImageIndex;
mActiveImageIndex = (1 + mActiveImageIndex) % mImageViews.length;
Log.d(TAG, "new active=" + mActiveImageIndex);
final ImageView activeImageView = mImageViews[mActiveImageIndex];
activeImageView.setAlpha(0.0f);
ImageView inactiveImageView = mImageViews[inactiveIndex];
animate(activeImageView);
AnimatorSet animatorSet = new AnimatorSet();
animatorSet.setDuration(mFadeInOutMs);
animatorSet.playTogether(
ObjectAnimator.ofFloat(inactiveImageView, "alpha", 1.0f, 0.0f),
ObjectAnimator.ofFloat(activeImageView, "alpha", 0.0f, 1.0f)
);
animatorSet.start();
}
private void start(View view, long duration, float fromScale, float toScale, float fromTranslationX, float fromTranslationY, float toTranslationX, float toTranslationY) {
view.setScaleX(fromScale);
view.setScaleY(fromScale);
view.setTranslationX(fromTranslationX);
view.setTranslationY(fromTranslationY);
ViewPropertyAnimator propertyAnimator = view.animate().translationX(toTranslationX).translationY(toTranslationY).scaleX(toScale).scaleY(toScale).setDuration(duration);
propertyAnimator.start();
Log.d(TAG, "starting Ken Burns animation " + propertyAnimator);
}
private float pickScale() {
return this.minScaleFactor + this.random.nextFloat() * (this.maxScaleFactor - this.minScaleFactor);
}
private float pickTranslation(int value, float ratio) {
return value * (ratio - 1.0f) * (this.random.nextFloat() - 0.5f);
}
public void animate(View view) {
float fromScale = pickScale();
float toScale = pickScale();
float fromTranslationX = pickTranslation(view.getWidth(), fromScale);
float fromTranslationY = pickTranslation(view.getHeight(), fromScale);
float toTranslationX = pickTranslation(view.getWidth(), toScale);
float toTranslationY = pickTranslation(view.getHeight(), toScale);
start(view, this.mSwapMs, fromScale, toScale, fromTranslationX, fromTranslationY, toTranslationX, toTranslationY);
}
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
startKenBurnsAnimation();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
mHandler.removeCallbacks(mSwapImageRunnable);
}
private void startKenBurnsAnimation() {
mHandler.post(mSwapImageRunnable);
}
@Override
protected void onFinishInflate() {
super.onFinishInflate();
View view = inflate(getContext(), R.layout.view_kenburns, this);
mImageViews = new ImageView[2];
mImageViews[0] = (ImageView) view.findViewById(R.id.image0);
mImageViews[1] = (ImageView) view.findViewById(R.id.image1);
}
private void fillImageViews() {
for (int i = 0; i < mImageViews.length; i++) {
mImageViews[i].setImageResource(mResourceIds[i]);
}
}
}
这是我在 Kitkat 设备上运行代码时得到的 logcat:
04-20 04:47:43.051 D/KenBurnsView? swapImage active=-1
04-20 04:47:43.051 D/KenBurnsView? starting Ken Burns animation android.view.ViewPropertyAnimator@5284ef64
04-20 04:47:43.879 I/Choreographer? Skipped 47 frames! The application may be doing too much work on its main thread.
04-20 04:47:45.315 I/Choreographer? Skipped 63 frames! The application may be doing too much work on its main thread.
04-20 04:47:46.331 I/Choreographer? Skipped 60 frames! The application may be doing too much work on its main thread.
04-20 04:47:47.335 I/Choreographer? Skipped 59 frames! The application may be doing too much work on its main thread.
04-20 04:47:48.203 I/Choreographer? Skipped 51 frames! The application may be doing too much work on its main thread.
04-20 04:47:49.175 I/Choreographer? Skipped 57 frames! The application may be doing too much work on its main thread.
04-20 04:47:50.235 I/Choreographer? Skipped 63 frames! The application may be doing too much work on its main thread.
04-20 04:47:51.287 I/Choreographer? Skipped 62 frames! The application may be doing too much work on its main thread.
04-20 04:47:52.303 I/Choreographer? Skipped 60 frames! The application may be doing too much work on its main thread.
04-20 04:47:53.347 I/Choreographer? Skipped 62 frames! The application may be doing too much work on its main thread.
04-20 04:47:54.303 I/Choreographer? Skipped 56 frames! The application may be doing too much work on its main thread.
04-20 04:47:55.227 I/Choreographer? Skipped 54 frames! The application may be doing too much work on its main thread.
04-20 04:47:56.263 I/Choreographer? Skipped 62 frames! The application may be doing too much work on its main thread.
04-20 04:47:57.315 I/Choreographer? Skipped 62 frames! The application may be doing too much work on its main thread.
04-20 04:47:58.159 I/Choreographer? Skipped 49 frames! The application may be doing too much work on its main thread.
04-20 04:47:59.315 I/Choreographer? Skipped 69 frames! The application may be doing too much work on its main thread.
04-20 04:48:00.287 I/Choreographer? Skipped 57 frames! The application may be doing too much work on its main thread.
04-20 04:48:01.095 I/Choreographer? Skipped 46 frames! The application may be doing too much work on its main thread.
04-20 04:48:02.131 I/Choreographer? Skipped 62 frames! The application may be doing too much work on its main thread.
04-20 04:48:03.419 I/Choreographer? Skipped 76 frames! The application may be doing too much work on its main thread.
04-20 04:48:04.531 D/KenBurnsView? swapImage active=1
04-20 04:48:04.531 D/KenBurnsView? new active=0
04-20 04:48:04.531 D/KenBurnsView? starting Ken Burns animation android.view.ViewPropertyAnimator@54e2e858
04-20 04:48:04.531 I/Choreographer? Skipped 66 frames! The application may be doing too much work on its main thread.
04-20 04:48:05.075 I/Choreographer? Skipped 31 frames! The application may be doing too much work on its main thread.
04-20 04:48:06.087 I/Choreographer? Skipped 34 frames! The application may be doing too much work on its main thread.
04-20 04:48:07.047 I/Choreographer? Skipped 30 frames! The application may be doing too much work on its main thread.
04-20 04:48:10.943 D/dalvikvm? GC_FOR_ALLOC freed 76K, 1% free 88450K/88652K, paused 3ms, total 3ms
04-20 04:48:10.999 D/dalvikvm? GC_FOR_ALLOC freed 35K, 1% free 95824K/96060K, paused 2ms, total 2ms
04-20 04:48:11.055 I/Choreographer? Skipped 34 frames! The application may be doing too much work on its main thread.