(1)Android中常见的三种绘图类
Paint(画笔)
图形绘制的相关方法:
文字绘制的相关方法
Canvas(画布)
Path(路径)
安卓中的绘图类:
1. paint
用法 : (1)创建画笔 mypaint = new Paint();
(2)设置属性 : mypaint.setColor(Color.RED);
mypaint.setStyle(Paint.Style.FILL);
mypaint.setStrokeWidth(5);
mypaint.setTextSize(36);
2. Canvas
Canvas 对象时ondraw的参数
使用 canvas对象画画 :
canvas.drawColor(Color.GREEN);
canvas.drawCircle(100,100,50,mypaint);
3. path
创建path 对象 : Path path = new Path();
指定起始位置 path.moveTo(400, 600);
画线:path.lineTo(500, 700); 参数为终点位置
在Activity中使用绘图类的步骤
1. 创建绘图类 new ->Java Class
2. 继承android.view.View : public class MyOwnView extends View {}
3. 编写构造方法
4. 重写ondraw方法
5. 把这个view类设置给Activity
MyOwnView mView = new MyOwnView(getApplicationContext());
setContentView(mView);
(1)Android中常见的动画
逐帧动画(Frame)
补间动画(Tween)
动画监听:
为View、Fragment动态设置动画效果
为activity设置转场动画
属性动画(Property)
Android中的动画类型
1. 逐帧动画 :
步骤: 1. 定义 在res下创建一个anim目录 编写anim文件。在这个文件中,定义各个静态图片和显示时间
<animation-list xmlns:android="<http://schemas.android.com/apk/res/android>"
android:oneshot="false">
<item
android:drawable="@mipmap/img_miao1"
android:duration="80" />
<item 。。。。>
</animation-list>
2. 定义imageView , 设置imageView的背景图片为刚才定义的anim文件
3. 在activity 中获取 AnimationDrawable对象
anim= (AnimationDrawable)img.getBackground();
4.调用start 和stop来控制动画的开始和结束
2. 补间动画
步骤 :
1. 定义 动画类型的XML文件,比如 透明度渐变 缩放渐变等等
2. 定义一个组件来显示这些渐变
3. 获取动画对象:animation = AnimationUtils.loadAnimation(this,R.anim.anim_alpha); -》 第二个参数为XML定义的渐变类型
4. 调用img.startAnimation(animation); 来启动动画
3. 属性动画:
步骤:
1. 定义组件来显示渐变
2. 获取动画对象 animator1 = ObjectAnimator.ofFloat(bt, "alpha", 1f, 0f, 1f, 0f, 1f);
3.启动动画 animator1.start();
(1)Android中常见的媒体API开发
利用SoundPool用来播放密集、急促而又短暂的音效
使用MediaRecord录音
利用MediaPlayer播放音频与视频
播放视频:MediaPlayer+SurfaceView、VideoView
SoundPool 使用步骤:
1. 创建SoundPool对象 :
SoundPool.Builder spb = new SoundPool.Builder();
spb.setMaxStreams(2);
sp = spb.build();
2. 加载音频文件
HashMap<Integer, Integer> soundID = new HashMap<Integer, Integer>();
soundID.put(1, sp.load(this,R.raw.biaobiao,1));
soundID.put(2, sp.load(this,R.raw.fly,1));
3. 播放音频文件
sp.play(soundID.get(1), 1, 1, 0, 0, 1);
4.释放音频资源
sp.release();
使用MediaPlayer的步骤:
1. 创建对象 : mp = android.media.MediaPlayer.create(this,R.raw.fly); -》第一个参数:当前环境 第二个参数:多媒体文件Id
本地文件路径:mp.setDataSource("/....")
网络URL文件:mp.setDataSource("URL")
2. 播放: mp.start();
3. 暂停: mp.pause();
4. 停止: mp.stop();
mp.release();
MediaRecord : 实现录音功能
1. 创建MediaRecord对象
mRecorder=new MediaRecorder();
2. 设定声音来源
mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
3. 设定输出格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
4. 设定编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
5. 保存文件
mRecorder.setOutputFile(path);
6.准备开始
mRecorder.prepare();
mRecorder.start();
7.结束
mRecorder.stop();
mRecorder.release();
注意事项 : 1. 赋权限 : 在清单文件中加入录音权限
<uses-permission android:name="android.permission.RECORD_AUDIO" />
2. 动态权限:
3. 需要打开手机模拟器的麦克风 -》 setting -》MicroPhone -》 开启
4. 打开电脑的麦克风 -》 设置 -》 隐私 -》 麦克风