本篇是介绍Drawable相关方面的第七篇博客了,本篇博客主要介绍RotateDrawable的简单用法。更多的、更复杂的RotateDrawable相关方面的东西就不在这里做介绍了(主要还是因为我不会!!)。
先看一下效果图吧:
废话不多说,直接上代码,稍后将根据代码讲解!!!
主布局文件:
<?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/activity_main"
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"
tools:context="com.example.rotatedrawable.MainActivity">
<ImageView
android:id="@+id/iv"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_centerHorizontal="true"
android:background="@color/colorAccent"
android:maxHeight="300dp"
android:maxWidth="400dp"
android:scaleType="fitXY"
android:src="@drawable/rotate_drawable" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/iv"
android:layout_marginTop="10dp"
android:onClick="setRotateDrawableLevel"
android:text="@string/set_rotate" />
</RelativeLayout>
在这里我将ImageView的背景设置为红色,drawable设置为图片显示来源,通过实例你就会发现,旋转的并不是imageView,仅仅只是drawable旋转了,然后再将旋转后的drawable重新输入到ImageView中。有背景色对比显得更清楚。。。
rotate_drawable文件:
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:visible="true"
android:pivotY="50%"
android:pivotX="50%"
android:fromDegrees="0"
android:toDegrees="180"
android:drawable="@drawable/first">
</rotate>
这里的pivotY、pivotX分别代表旋转中心的位置,50%代表drawable的中心为旋转中心。toDegrees代表最大可以旋转的角度。在代码中是通过level值确定的,这两个值之间存在对应关系。如果设置最大旋转角度为180,那么Level=10000代表的就是旋转180度,5000代表旋转90度,如果最大旋转角度设置为270度,那么level=5000,就代表要旋转135度。可以自己尝试一下!!!(都是顺时针旋转)。
activity文件:
package com.example.rotatedrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.RotateDrawable;
import android.os.CountDownTimer;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private ImageView iv;
private Drawable drawable;
private final int UPDATE_VIEW = 99;
private boolean IS_ROTATING = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.iv);
drawable = iv.getDrawable();
drawable.setLevel(0);
}
public void setRotateDrawableLevel(View view){
if (drawable instanceof RotateDrawable){
// drawable.setLevel(5000);
IS_ROTATING = true;
if(IS_ROTATING){
timer.start();
}
}
}
private CountDownTimer timer = new CountDownTimer(Integer.MAX_VALUE,60) {
@Override
public void onTick(long millisUntilFinished) {
mHandler.sendEmptyMessage(UPDATE_VIEW);
}
@Override
public void onFinish() {
IS_ROTATING = false;
}
};
private Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case UPDATE_VIEW:
int level = drawable.getLevel();
Log.i("zyq","level = "+level);
if (level >= 10000){
timer.cancel();
drawable.setLevel(10000);
}else{
drawable.setLevel(level+100);
}
break;
}
}
};
}
在代码中通过定时器和handler实现UI刷新,具体的就不做过多的解释了,代码很简单,相信这么聪明的你肯定一眼就看懂了(别喷我呀)!!!
这是我的微信公众号,如果可以的话,希望您可以关注一下,这将是对我最大的鼓励了,谢谢!!!
代码地址:
代码在GitHub上,代码都是在晚上11点左右才会push到GitHub上的,如果没有看待,那就说明我还没有传上去!!!