2014303413_肖明睿_实验7
- 格式:doc
- 大小:962.00 KB
- 文档页数:26
训练七 图形与图像处理
7.1 实验目的
掌握Android绘图API,包括Canvas、Paint、Path等类,掌握Android绘图的双缓冲机制、利用Matrix对图形进行几何变换,掌握帧动画、补间动画、属性动画,了解SurfaceView的绘图机制。
7.2 实验内容和要求
1) 学习使用简单图片;
2) 学习掌握Android绘图;
3) 学习图形特效处理;
4) 学习逐帧(frame)动画;
5) 学习补间(tween)动画;
6) 学习属性动画;
7) 学习使用surfaceview实现动画。
7.3 仪器设备
1) PC机最低配置:2G Hz以上双核CPU,2G以上内存,1G自由硬盘空间;
2) WindowsXP(32位)或Vista(32或64位)或Windows7(32或64位);
3) Eclipse;
4) Eclipse JDT 插件;
5) JDK 6或JDK 7;
6) Android Development Tools插件。
7.4 实验主要步骤
2 阅读资料了解Android的图形处理基础;
Android系统提供了imageview显示普通静态图片,也提供了animationdrawable来开发逐帧动画,还可以通过animation对普通图片使用补减动画。图形、图像处理不仅是android系统的应用界面非常重要,而且android系统上益智游戏、2D游戏都需要大量的图形,图像处理。所谓游戏,本质就是提供更逼真的、能够模拟某种环境的用户界面,并根据某种规则来响应用户操作。为了提供更逼真的用户界面,需要借助于图形处理。
3 了解Bitmap与BitmapFactory;
import java.io.IOException;
import java.io.InputStream;
import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class BitmapTest extends Activity
{
String[] images = null;
AssetManager assets = null;
int currentImg = 0;
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(yout.main);
image = (ImageView) findViewById(R.id.image);
try
{
assets = getAssets();
images = assets.list("");
System.out.println(images.length);
}
catch (IOException e)
{
e.printStackTrace();
}
final Button next = (Button) findViewById(R.id.next);
next.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View sources) {
if (currentImg >= images.length)
{
currentImg = 0;
}
while (!images[currentImg].endsWith(".png")
&& !images[currentImg].endsWith(".jpg")
&& !images[currentImg].endsWith(".gif"))
{
currentImg++;
if (currentImg >= images.length)
{
currentImg = 0;
}
}
InputStream assetFile = null;
try
{
assetFile = assets.open(images[currentImg++]);
}
catch (IOException e)
{
e.printStackTrace();
}
BitmapDrawable bitmapDrawable = (BitmapDrawable) image
.getDrawable();
if (bitmapDrawable != null
&& !bitmapDrawable.getBitmap().isRecycled()) //①
{
bitmapDrawable.getBitmap().recycle();
}
image.setImageBitmap(BitmapFactory
.decodeStream(assetFile)); //②
}
});
}
}
xmlns:android="/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
android:id="@+id/next"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/next"
/>
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
/>
4 继承view在Android中绘图;
publicclass CanvasTest extends Activity {
@Override
publicvoid onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(yout.main);
}
}
publicclass MyView extends View
{
public MyView(Context context, AttributeSet set)
{
super(context, set);
}
@Override
protectedvoid onDraw(Canvas canvas)
{
super.onDraw(canvas);
canvas.drawColor(Color.WHITE);
Paint paint = new Paint();
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(3);
canvas.drawCircle(40, 40, 30, paint);
canvas.drawRect(10, 80, 70, 140, paint);
canvas.drawRect(10, 150, 70, 190, paint);
RectF re1 = new RectF(10, 200, 70, 230);
canvas.drawRoundRect(re1, 15, 15, paint);
RectF re11 = new RectF(10, 240, 70, 270);
canvas.drawOval(re11, paint);
Path path1 = new Path();
path1.moveTo(10, 340);
path1.lineTo(70, 340);
path1.lineTo(40, 290);
path1.close();
canvas.drawPath(path1, paint);
Path path2 = new Path();
path2.moveTo(26, 360);
path2.lineTo(54, 360);
path2.lineTo(70, 392);
path2.lineTo(40, 420);