Rotatedrawable详解

对另一个drawable资源,基于当前的level,进行旋转的drawable

语法


元素:

<rotate>

定义一个RotateDrawable,必须作为根元素。

属性:

xmlns:android
String类型。 必须的。定义XML文件的命名空间。必须是 "http://schemas.android.com/apk/res/android".
android:drawable

Drawable 资源。必须的。引用一个drawable资源。

android:visible
Boolean。是否可见。
android:fromDegrees
整形。  从多少的角度开始旋转

android:toDegrees
整形。  到多少的角度结束旋转

android:pivotX
百分比。  旋转的中心在图片X轴的百分比

android:visible
百分比。  旋转的中心在图片Y轴的百分比
代码示例

自定义一个rotatedrawable.xml

<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@mipmap/arrow1"
    android:fromDegrees="-230"
    android:toDegrees="50"
    android:pivotX="50%"
    android:pivotY="50%"
  ></rotate>
在布局中使用

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.clipdrawable.MainActivity">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:src="@mipmap/biaopan" />
        <ImageView
            android:id="@+id/rotateimg"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_centerInParent="true"
            android:onClick="onClick"
            android:src="@drawable/rotatedrawable" />
    </RelativeLayout>
</LinearLayout>
在activity中

package com.example.clipdrawable;
import android.graphics.drawable.RotateDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
    private int rotatelevel = 0;
    ImageView rotateimg;
    RotateDrawable rotateDrawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         rotateimg = (ImageView) findViewById(R.id.rotateimg);
         rotateDrawable = (RotateDrawable) rotateimg.getDrawable();
        rotateimg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (rotatelevel == 10000) {
                    rotateimg.post(rdisapper);
                } else {
                    rotateimg.post(rshow);
                }
            }
        });
    }
    //开启一个增加的线程
    private Runnable rshow = new Runnable() {
        public void run() {
            if (rotatelevel < 10000) {
                rotatelevel += 100;
                rotateDrawable.setLevel(rotatelevel);
                rotateimg.postDelayed(rshow, 100);
            }
        }
    };
    //开启一个减小的进程
    private Runnable rdisapper = new Runnable() {
        @Override
        public void run() {
            if (rotatelevel > 100) {
                rotatelevel -= 100;
                rotateDrawable.setLevel(rotatelevel);
                rotateimg.postDelayed(rdisapper, 100);
            }
        }
    };
}
效果就是一个指针在表盘中点击转一圈,然后在点击一次就回去了


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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值