Implementation of Graphics and Animation in Android

1. Drawing of simple graphics
       
        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. The playback of custom animations
  There are two main animation modes in Android, one is tweenedanimation (gradient animation), that is, the animation effect is generated by continuously performing image transformation on the objects of the scene; the other is framebyframe (frame animation), that is Plays preconfigured animation frames in sequence. The following is mainly the application of gradient animation.
  First create the animation file donghuaanim.xml:

 
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Transparency Transform-->
    <alpha
        android:fromAlpha="0.1"
        android:toAlpha="1.0"
        android:duration="2000"/>

    <!-- Size Transformation-->
    <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"/>

    <!-- Position Transformation-->
    <translate
        android:fromXDelta="30"
        android:toXDelta="0"
        android:fromYDelta="30"
        android:toYDelta="50"
        android:duration="4000"/>

    <!-- Rotation Transformation-->
    <rotate
        android:interpolator="@android:anim/accelerate_decelerate_interpolator"
        android:fromDegrees="0"
        android:toDegrees="+350"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="5000"/>

</set>

  Add the layout file 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>

  Add DonghuaActivity.java file:
 
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);
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326444952&siteId=291194637