使用ObjectAnimator顺时针旋转ImageView

11

我想将一张图片旋转,以便它能够适当地放置在另一张图片下方。这是我想要实现的效果:

What I am trying to achieve

而这是我当前实现的效果:

Currently achieved

它基本上包含两个 ImageView,其中第一个包含地板上面的部分,第二个包含覆盖地板的瓷砖。

我的问题是,我无法使瓷砖图像顺时针旋转并围绕 x 轴旋转视图,使其适合位于包含地板上面部分的图片下方。

我正在使用 ObjectAnimator 让我的 ImageView 动画化。以下是我的代码:

public class VisualizerFragment extends Fragment {

ImageView image;
FlipImageView image_back;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    View view = inflater.inflate(R.layout.fragment_visualizer, container, false);
    image = (ImageView) view.findViewById(R.id.vs_image);
    image_back = (FlipImageView) view.findViewById(R.id.vs_image_back);

    Glide.with(getActivity())
            .load(R.drawable.floor3)
            .centerCrop()
            .fitCenter()
            .listener(new RequestListener<Integer, GlideDrawable>() {
                @Override
                public boolean onException(Exception e, Integer model, Target<GlideDrawable> target, boolean isFirstResource) {
                    return false;
                }

                @Override
                public boolean onResourceReady(GlideDrawable resource, Integer model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
                    Bitmap bmp = BitmapFactory.decodeResource(getActivity().getResources(), R.drawable.i9004);

                    //crop image
                    Matrix matrix = new Matrix();
                    matrix.postScale(1f, 1f);
                    Bitmap croppedBitmap = Bitmap.createBitmap(bmp, 0, 0,100, 100, matrix, true);

                    bmp = getRoundedCornerBitmap(croppedBitmap, getResources().getColor(R.color.line),0,1,getActivity());
                    BitmapDrawable bitmapDrawable = new BitmapDrawable(bmp);
                    bitmapDrawable.setTileModeXY(Shader.TileMode.REPEAT, Shader.TileMode.REPEAT);

                    image_back.setDrawable(bitmapDrawable);
                    image_back.setFlippedDrawable(bitmapDrawable);
                    image_back.setFlipped(true);
                    image_back.toggleFlip();

                    //rotate image
                    ObjectAnimator anim = (ObjectAnimator) AnimatorInflater.loadAnimator(getActivity(), R.animator.fliping);
                    anim.setTarget(image_back);
                    anim.setDuration(100);
                    anim.start();


                    return false;

                }
            })
            .into(image);


    return view;
}


public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int color, int cornerDips, int borderDips, Context context) {
    Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(),
            Bitmap.Config.ARGB_8888);
    Canvas canvas = new Canvas(output);

    final int borderSizePx = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) borderDips,
            context.getResources().getDisplayMetrics());
    final int cornerSizePx = 0;
    final Paint paint = new Paint();
    final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
    final RectF rectF = new RectF(rect);

    // prepare canvas for transfer

    paint.setAntiAlias(true);
    paint.setColor(0xFFFFFFFF);
    paint.setStyle(Paint.Style.FILL);
    canvas.drawARGB(0, 0, 0, 0);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    // draw bitmap
    paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
    canvas.drawBitmap(bitmap, rect, rect, paint);


    // draw border
    paint.setColor(color);
    paint.setStyle(Paint.Style.STROKE);
    paint.setStrokeWidth((float) borderSizePx);
    canvas.drawRoundRect(rectF, cornerSizePx, cornerSizePx, paint);

    return output;
}
}

下面是我用于动画的flipping.xml:

<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
    android:propertyName="rotationX"
    android:valueFrom="20"
    android:valueTo="50"
    android:valueType="floatType"
/>

你是否遇到了错误?它是否在旋转,还是什么都没有发生? - A Honey Bustard
抱歉,我不太明白你的意思,请再详细解释一下。 - Elltz
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - H Raval
你尝试过创建一个更大的瓷砖图像(因此,基本上是w和h ==斜边),使其比任何旋转时的窗口都要大,然后使用剪辑矩形来不绘制屏幕外的区域吗? - Submersed
2个回答

3

我已经找到了解决方案...移除对象动画并使用以下代码:

camera.save();

            camera.rotateX(790);
            camera.rotateY(2.5f);
            camera.rotateZ(140);

            camera.translate(170,0, -8);

            camera.getMatrix(matrix);
            camera.restore();

创建您自己的自定义图像类,并在 applyTransformation() 中使用它。

0

在简单加载图像后尝试以下代码

        ObjectAnimator imageViewObjectAnimator = ObjectAnimator.ofFloat(imageview ,
                "rotation", 20f, 50f);
        imageViewObjectAnimator.setDuration(1000); // miliseconds
        imageViewObjectAnimator.start();

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接