亲宝软件园·资讯

展开

Android绘制文本与图片及动效详解

上后左爱 人气:0

绘制文本

绘制类 借助 Canvas 类进行绘制

通过重写View.onDraw 方法来进行旁白描绘

package com.mingrisoft;
import android.app.Fragment;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Shader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout fragment = (FrameLayout) findViewById(R.id.frameLayout);  //获取帧布局管理器
        fragment.addView(new MyView(this));  //将自定义视图的内部类添加到布局管理器中
    }
    private class MyView extends View {
        public MyView(Context context) {
            super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {  //重写onDraw()方法
            Paint paint = new Paint();           //定义一个默认的画笔
            //线性渐变
            Shader shader = new LinearGradient(0, 0, 100, 100,
                    Color.RED, Color.GREEN, Shader.TileMode.MIRROR);
            paint.setShader(shader);                            //为画笔设置渐变器
            canvas.drawRect(10, 10, 280, 150, paint);                //绘制矩形
            super.onDraw(canvas);
        }
    }
}

绘制图片

创建位图对象, JAVA 中bitmap 表示位图对象

创建位图对象 两种方式:

BitMapFactory :

BitMap类挖取一块图像创建,将源位图缩放生成,使用颜色数组创建

BitMapFactory:

1.decodeFile,

2.decodeResource,

3.decodeStream

访问SDK资源需要在Mainiefiest.xml 配置访问权限 

<uses-premission android:name="android.permission.READ_EXTERNAL_STOTAHE">

同时在Android 上配置访问权限

package com.example.myapplication;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
/**
  * 手势方式滑动动画
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout);  //获取帧布局管理器
        frameLayout.addView(new MyView(this));  //将自定义视图的内部类添加到布局管理器中
    }
    }
    class MyView extends View {
        public MyView(Context context) {
            super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            // 获取SDK上 图片绘制
            String path  = Environment.getExternalStorageDirectory() + "/snail.png";
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            //通过 canvas 类 绘制图片
            Paint paint = new Paint(); //定义
            canvas.drawBitmap(bitmap,150,150, paint);
            // 从图像中挖取一小片 bitMap 类 的 createBitMap(截取一小块), compress(压缩)
            // 从原始图像挖取小部分
            Bitmap bitmap1 = Bitmap.createBitmap(bitmap,23,89,150,168);
            canvas.drawBitmap(bitmap1,270,50, paint);
        }
    }

绘制路径

package com.example.myapplication;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import androidx.appcompat.app.AppCompatActivity;
/**
  *
 */
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FrameLayout frameLayout= (FrameLayout) findViewById(R.id.frameLayout);  //获取帧布局管理器
        frameLayout.addView(new MyView(this));  //将自定义视图的内部类添加到布局管理器中
    }
    }
    /**
     *  绘制路径
     * */
    class MyView extends View {
        public MyView(Context context) {
            super(context);
        }
        @Override
        protected void onDraw(Canvas canvas) {
            super.onDraw(canvas);
            Paint paint = new Paint();
            paint.setAntiAlias(true) ; // 定义抗锯齿
            paint.setColor(0xFF0000FF);
            paint.setStyle(Paint.Style.STROKE); // 设置样式为描边
            Path path = new Path();
            // 最后一个参数环按照 顺时针还是
            path.addCircle(150,100,50,Path.Direction.CW);
          //  canvas.drawPath(path,paint);
            canvas.drawTextOnPath("活着就是为了修改时间", path,0,0,paint);
        }
    }

逐帧动画

创建动画资源文件 在drawable 目录下面

使用 animation-list
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/img001" android:duration="60"/>
    <item android:drawable="@drawable/img002" android:duration="60"/>
    <item android:drawable="@drawable/img003" android:duration="60"/>
    <item android:drawable="@drawable/img004" android:duration="60"/>
    <item android:drawable="@drawable/img005" android:duration="60"/>
    <item android:drawable="@drawable/img006" android:duration="60"/>
</animation-list>

MainActivity 获取AnminationDrawable 方法设置动态图逐帧播放

package com.example.myapplication;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import androidx.appcompat.app.AppCompatActivity;
/**
  *
 */
public class MainActivity extends AppCompatActivity {
    private boolean flag = true;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        LinearLayout linearLayout= (LinearLayout) findViewById(R.id.linearLayout); //获取布局管理器
        //获取AnimationDrawable对象
        final AnimationDrawable anim = (AnimationDrawable) linearLayout.getBackground();
        linearLayout.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                if(flag){
                    anim.start();
                    flag = false;
                }else{
                    anim.stop();
                    flag = true;
                }
            }
        });
    }
    }

补间动画

参考手势时候 设置动画资源文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<alpha android:fromAlpha="0"
	    android:toAlpha="1"
	    android:duration="4000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
	<alpha android:fromAlpha="1"
	    android:toAlpha="0"
	    android:duration="2000"/>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="0.0"
        android:fromYScale="0.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="1.0"
        android:toYScale="1.0"
        android:duration="2000"></scale>
</set>
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <scale android:fromXScale="1.0"
        android:fromYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toXScale="0.0"
        android:toYScale="0.0"
        android:duration="2000"></scale>
</set>
package com.example.myapplication;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.ViewFlipper;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener {
    ViewFlipper flipper; //定义ViewFlipper
    GestureDetector detector; //定义手势检测器
    Animation[] animation = new Animation[4];//定义动画数组,为ViewFlipper指定切换动画
    final int distance = 50; //定义手势动作两点之间最小距离
    //定义图片数组
    private int[] images = new int[]{R.drawable.img01, R.drawable.img02, R.drawable.img03,
            R.drawable.img04, R.drawable.img05, R.drawable.img06, R.drawable.img07, R.drawable.img08,
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        detector = new GestureDetector(this, this); //创建手势检测器
        flipper = (ViewFlipper) findViewById(R.id.flipper); //获取ViewFlipper
        for (int i = 0; i < images.length; i++) {
            ImageView imageView = new ImageView(this);
            imageView.setImageResource(images[i]);
            flipper.addView(imageView); //加载图片
        }
        //初始化动画数组
        animation[0] = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_in);     //淡入动画
        animation[1] = AnimationUtils.loadAnimation(this, R.anim.anim_alpha_out);      //淡出动画
        animation[2] = AnimationUtils.loadAnimation(this, R.anim.anim_scale_in);      //放大进入动画
        animation[3] = AnimationUtils.loadAnimation(this, R.anim.anim_scale_out);  //缩小退出动画
    }
    @Override
    public boolean onDown(MotionEvent e) {
        return false;
    }
    @Override
    public void onShowPress(MotionEvent e) {
    }
    @Override
    public boolean onSingleTapUp(MotionEvent e) {
        return false;
    }
    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        return false;
    }
    @Override
    public void onLongPress(MotionEvent e) {
    }
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        /*
        如果第一个触点事件的X坐标到第二个触点事件的X坐标的距离超过distance就是从右向左滑动
         */
        if (e1.getX() - e2.getX() > distance) {
            //为flipper设置切换的动画效果
            flipper.setInAnimation(animation[0]);
            flipper.setOutAnimation(animation[1]);
            flipper.showPrevious();
            return true;
            /*
            如果第二个触点事件的X坐标到第一个触点事件的X坐标的距离超过distance就是从左向右滑动
             */
        } else if (e2.getX() - e1.getX() > distance) {
            //为flipper设置切换的动画
            flipper.setInAnimation(animation[2]);
            flipper.setOutAnimation(animation[3]);
            flipper.showNext();
            return true;
        }
        return false;
    }
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        //将该Activity上的触碰事件交给GestureDetector处理
        return detector.onTouchEvent(event);
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论