canvas.drawColor(Color.BLUE); Paint paint = new Paint(); paint.setColor(Color.RED); canvas.drawRect(10, 10, 110, 110, paint); canvas.drawText("This is text", 10, 130, paint); RectF rf1 = new RectF(10, 130, 110, 230); canvas.drawArc(rf1, 0, 45, true, paint); canvas.drawLine(150, 10, 250, 110, paint); RectF rf2 = new RectF(150, 130, 250, 230); canvas.drawOval(rf2, paint);
2、自定义动画的播放
Android中主要有两种动画模式,一种是tweenedanimation(渐变动画),即通过对场景的对象不断做图像变换产生动画效果;另一种是framebyframe(帧动画),即按照顺序播放事先配置好的动画帧。下面主要是渐变动画的应用。
首先创建动画文件donghuaanim.xml:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 透明度变换 --> <alpha android:fromAlpha="0.1" android:toAlpha="1.0" android:duration="2000"/> <!-- 尺寸变换 --> <scale android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromXScale="0.0" android:toXScale="1.4" android:fromYScale="0.0" android:toYScale="1.4" android:pivotX="50%" android:pivotY="50%" android:fillAfter="false" android:duration="3000"/> <!-- 位置变换 --> <translate android:fromXDelta="30" android:toXDelta="0" android:fromYDelta="30" android:toYDelta="50" android:duration="4000"/> <!-- 旋转变换 --> <rotate android:interpolator="@android:anim/accelerate_decelerate_interpolator" android:fromDegrees="0" android:toDegrees="+350" android:pivotX="50%" android:pivotY="50%" android:duration="5000"/> </set>
添加布局文件donghua_xml.xml:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/imageView" android:src="@mipmap/image"/> </LinearLayout>
添加DonghuaActivity.java文件:
package xiao.fuyan.testapp; import android.app.Activity; import android.os.Bundle; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; /** * Created by xiao on 2017/1/10. */ public class DonghuaActivity extends Activity { Animation animation; ImageView imageView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.donghua_xml); animation = AnimationUtils.loadAnimation(this, R.anim.donghuaanim); imageView = (ImageView) findViewById(R.id.imageView); imageView.startAnimation(animation); } }