Android studio 实现图片旋转效果

这个图片翻转效果有些类似于支付宝五福翻转的效果,或者是游戏中的翻拍效果先附上效果图


第一步是两个正面反面的布局很简单就是一个ImageView和Textview组成我拿其中一个举例

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_fl_card_front"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:contentDescription="@null"
        android:padding="16dp"
        android:src="@mipmap/two"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:textSize="20sp"
        android:text="大姑娘"
        android:textColor="@android:color/white"/>
</RelativeLayout>

第二部主页布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_fl_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:onClick="fipCard"
    tools:context="com.example.administrator.cardflip.MainActivity">

    <include layout="@layout/cell_card_front"></include>
    <include layout="@layout/cell_card_back"></include>


</RelativeLayout>
第三部 书写属性动画

动画出

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--旋转-->
    <objectAnimator android:duration="3000"
        android:propertyName="rotationY"
        android:valueFrom="0"
        android:valueTo="180dp">

    </objectAnimator>

    <objectAnimator android:duration="0"
        android:propertyName="alpha"
        android:startOffset="1500"
        android:valueFrom="1.0"
        android:valueTo="0.0">

    </objectAnimator>
动画进

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<!--消失-->
    <objectAnimator android:duration="0"
        android:propertyName="alpha"
        android:valueFrom="1.0"
        android:valueTo="0.0"></objectAnimator>
    <!--旋转-->
    <objectAnimator android:duration="3000"
        android:propertyName="rotationY"
        android:valueFrom="-180"
        android:valueTo="0"></objectAnimator>
    <!--出现-->
    <objectAnimator android:duration="0"
        android:propertyName="alpha"
        android:startOffset="1500"
        android:valueFrom="0.0"
        android:valueTo="1.0"></objectAnimator>
</set>

第四部整合代码实现效果

public class MainActivity extends AppCompatActivity {
    AnimatorSet mRightOutSet, mLeftOutSet;
    boolean mIsShowBack;
    @BindView(R.id.main_fl_card_back)
    RelativeLayout mainFlCardBack;
    @BindView(R.id.main_fl_card_front)
    RelativeLayout mainFlCardFront;
    @BindView(R.id.main_fl_container)
    RelativeLayout mainFlContainer;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);
        setAnimators();//设置动画
        setCameraDistance();//设置距离


    }

    //设置动画
    private void setAnimators() {
        mRightOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_out);
        mLeftOutSet = (AnimatorSet) AnimatorInflater.loadAnimator(this, R.animator.anim_in);
        //设置点击事件
        mRightOutSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mainFlContainer.setClickable(false);
            }
        });

        mLeftOutSet.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationStart(Animator animation) {
                super.onAnimationStart(animation);
                mainFlContainer.setClickable(true);
            }
        });

    }

    //改变视角距离,贴近屏幕
    private void setCameraDistance() {
        int distance = 16000;
        float scale = getResources().getDisplayMetrics().density * distance;
        mainFlCardFront.setCameraDistance(scale);
        mainFlCardBack.setCameraDistance(scale);
    }

    //点击卡片翻转卡片
    public void fipCard(View view) {
        //正面朝上
        if (!mIsShowBack) {
            mRightOutSet.setTarget(mainFlCardBack);
            mLeftOutSet.setTarget(mainFlCardFront);
            mRightOutSet.start();
            mLeftOutSet.start();
            mIsShowBack = true;
        } else {
            mRightOutSet.setTarget(mainFlCardFront);
            mLeftOutSet.setTarget(mainFlCardBack);
            mRightOutSet.start();
            mLeftOutSet.start();
            mIsShowBack = false;


        }
    }



}

希望对你有帮助


项目地址:http://download.csdn.net/detail/liufatao/9750898





### 回答1: 要在Android Studio中旋转图像,您可以使用以下步骤: 1. 将图像文件添加到您的项目中。 2. 在您的布局文件中添加一个ImageView元素,并将其设置为显示您的图像文件。 3. 在您的Java代码中,使用BitmapFactory类的decodeFile方法加载图像文件。 4. 使用Matrix类创建一个旋转矩阵。 5. 使用Bitmap类的createBitmap方法创建一个旋转后的图像。 6. 将旋转后的图像设置为ImageView元素的图像。 以下是一个示例代码片段,演示如何在Android Studio中旋转图像: ``` // 加载图像文件 Bitmap bitmap = BitmapFactory.decodeFile("path/to/image.jpg"); // 创建旋转矩阵 Matrix matrix = new Matrix(); matrix.postRotate(90); // 创建旋转后的图像 Bitmap rotatedBitmap = Bitmap.createBitmap(bitmap, , , bitmap.getWidth(), bitmap.getHeight(), matrix, true); // 设置旋转后的图像为ImageView元素的图像 ImageView imageView = findViewById(R.id.imageView); imageView.setImageBitmap(rotatedBitmap); ``` 请注意,这只是一个示例代码片段,您需要根据您的项目需求进行修改和调整。 ### 回答2: 在Android开发中,常常需要对图片进行旋转操作,以满足不同的需求。为此,我们可以借助Android Studio提供的API进行实现。 首先,我们需要引入相关的库文件,以支持图片处理的功能。具体的方法是在build.gradle文件中添加如下代码: ``` dependencies { implementation 'com.android.support:exifinterface:28.0.0' implementation 'com.android.support:palette-v7:28.0.0' } ``` 接着,定义一个方法用于获取图片的旋转角度,可以通过调用ExifInterface类的getRotationDegrees()方法实现。如下所示: ``` public static int getImageRotation(String imagePath) { int rotation = 0; try { ExifInterface exif = new ExifInterface(imagePath); int exifOrientation = exif.getAttributeInt( ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); switch (exifOrientation) { case ExifInterface.ORIENTATION_ROTATE_90: rotation = 90; break; case ExifInterface.ORIENTATION_ROTATE_180: rotation = 180; break; case ExifInterface.ORIENTATION_ROTATE_270: rotation = 270; break; } } catch (Exception e) { e.printStackTrace(); } return rotation; } ``` 其中,imagePath参数表示图片的路径,getRotationDegrees()方法会返回一个整型值,表示图片的旋转角度。 然后,在我们需要旋转图片的地方,可以使用Matrix类进行操作。具体的方法是实例化一个Matrix对象,通过调用Matrix类的preRotate()方法,向其传递旋转角度,来对图片进行旋转。如下所示: ``` Matrix matrix = new Matrix(); matrix.preRotate(angle); Bitmap rotatedBitmap = Bitmap.createBitmap( originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); ``` 其中,angle表示旋转的角度,originalBitmap表示需要旋转的原始图片,rotatedBitmap表示旋转后的图像。 最后,我们将旋转后的图片显示出来即可。完整的代码如下所示: ``` public static Bitmap rotateImage(String imagePath, Bitmap originalBitmap) { int angle = getImageRotation(imagePath); if (angle == 0) { return originalBitmap; } Matrix matrix = new Matrix(); matrix.preRotate(angle); Bitmap rotatedBitmap = Bitmap.createBitmap( originalBitmap, 0, 0, originalBitmap.getWidth(), originalBitmap.getHeight(), matrix, true); return rotatedBitmap; } ``` 通过以上的步骤,我们就可以轻松地实现Android Studio图片旋转操作,满足不同的需求。 ### 回答3: 在Android开发中,图片旋转是一项非常常见的操作,它可以使图片以某个角度进行旋转。Android Studio提供了一种非常方便的方式来实现图片旋转,我们只需要使用ImageView组件,设置它的rotation属性即可。 1、在layout文件中,添加一个ImageView组件,设置其宽高和图片资源: <ImageView android:id="@+id/imageview" android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/image"/> 2、在java文件中,找到ImageView组件并设置其旋转角度,例如: ImageView imageView = (ImageView)findViewById(R.id.imageview); imageView.setRotation(90); 这样就可以使图片旋转90度。如果想要使图片逆时针旋转,只需要将旋转角度设置为负数即可。 另外,如果需要实现更细致的图片旋转效果,还可以使用animate()方法来实现。例如,使用以下代码可以使图片从当前角度逐渐旋转到目标角度: ImageView imageView = (ImageView)findViewById(R.id.imageview); imageView.animate().rotation(90).start(); 总的来说,Android Studio提供了非常方便的方式来实现图片旋转操作,我们只需要简单地设置ImageView组件的rotation属性即可。如果需要实现更细致的效果,可以使用animate()方法。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值