gpt4 book ai didi

android - 使用 ObjectAnimator 顺时针旋转 ImageView

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:48:25 25 4
gpt4 key购买 nike

我正在尝试旋转一个图像,以便它恰好适合另一个图像的下方。所以这就是我想要实现的目标:

What I am trying to achieve

这就是我能够实现的目标:

Currently achieved

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

我的问题是我无法按顺时针方向为瓷砖图像制作动画,也无法围绕 x 轴旋转 View ,使其适合包含地板上方部分的图像下方。

我正在使用 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;
}
}

下面是我用于动画的 fliping.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"
/>

最佳答案

好吧,我已经找到了一个解决方案....删除对象动画器并改用此代码

camera.save();

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

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

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

创建自定义图像类并在 applyTransformation() 中使用它

关于android - 使用 ObjectAnimator 顺时针旋转 ImageView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38541897/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com