android 倾斜view 手动转动动画 随手势左右转动

32 篇文章 5 订阅
16 篇文章 0 订阅

 

 

activity:

package com.manss.myapplication

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.MotionEvent
import android.view.View
import android.view.View.OnTouchListener
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity

/**
 * View转动activity
 */
class RotateActivity : AppCompatActivity(), OnTouchListener {
    private var mRoot: RelativeLayout? = null
    private var mCircle: RelativeLayout? = null
    var i = 0
    var viewRotation = 0f
    var fingerRotation = 0.0
    var newFingerRotation = 0.0
    private var mImage: View? = null
    @SuppressLint("ClickableViewAccessibility")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_rotate_image)
        mRoot = findViewById(R.id.root)
        mCircle = findViewById(R.id.circle)
        mImage = findViewById(R.id.rotate_image_image)
        mRoot?.setOnTouchListener(this)
        val mIndex = intArrayOf(0)

    }

    override fun onTouch(v: View, event: MotionEvent): Boolean {
        val x = event.x
        val y = event.y
        //以控件中心为旋转中心
        val xc = mRoot!!.width / 2.toFloat()
        val yc = mRoot!!.height / 2.toFloat()
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                viewRotation = mCircle!!.rotation //记录当前点击时的角度
                fingerRotation = Math.toDegrees(
                    Math.atan2(
                        x - xc.toDouble(),
                        yc - y.toDouble()
                    )
                )
            }
            MotionEvent.ACTION_MOVE -> {
                newFingerRotation = Math.toDegrees(
                    Math.atan2(
                        x - xc.toDouble(),
                        yc - y.toDouble()
                    )
                )
                mCircle!!.rotation = (viewRotation + newFingerRotation - fingerRotation).toFloat()
            }
            MotionEvent.ACTION_UP -> {
                newFingerRotation = 0.0
                fingerRotation = newFingerRotation
            }
        }
        return true
    }
}
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:id="@+id/root"
        android:layout_height="match_parent">

        <RelativeLayout
            android:layout_width="300dp"
            android:layout_centerInParent="true"
            android:id="@+id/circle"
            android:rotationX="25"
            android:layout_height="300dp">

            <ImageView
                android:id="@+id/rotate_image_image"
                android:layout_width="300dp"
                android:layout_height="300dp"
                android:layout_centerInParent="true"
                android:src="@mipmap/circle0"/>

            <TextView
                android:layout_width="wrap_content"
                android:text="@string/app_name"
                android:layout_centerInParent="true"
                android:textColor="@android:color/black"
                android:layout_height="wrap_content"/>

        </RelativeLayout>

    </RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>

 

activity_rotate_image.xml文件

android:rotationX="25"

表示绕X轴旋转25度,达到倾斜效果

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值