private static class SampleView extends View {
private Bitmap mBitmap;
private Bitmap mBitmap2;
private Bitmap mBitmap3;
private Shader mShader;
private static void drawIntoBitmap(Bitmap bm) {
float x = bm.getWidth();
float y = bm.getHeight();
Canvas c = new Canvas(bm);
Paint p = new Paint();
/* Paint类的一个边缘光滑的方法,true表示边缘光滑*/
p.setAntiAlias(true);
p.setAlpha(0x80);/设置颜色透明度为十六进制80(半透明),0x00全透明,0xFF不透明
/*在位图矩阵区域内画一个相切的圆*/
c.drawCircle(x/2, y/2, x/2, p);
p.setAlpha(0x30);
/*用指定的PorterDuff模型创建xformode,PorterDuff.Mode.SRC
* 表示下面要绘制的文本应在上面绘制的圆的上层
*/
p.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
p.setTextSize(60);
/*Paint.Align 是文本对齐方式的一个枚举类
* CENTER表示文本居中
* LEFT 表示做对齐
* RIGHT 表示右对齐
*/
p.setTextAlign(Paint.Align.CENTER);
/*FontMetrics是字体度量的类描述了给定文本大小的各种各样的字体度量。
* ascent 表示到基准线之上的距离
* bottom 表示到基准线之下的最大距离,它是最低的字体类型
* descent 表示到基准线之下的距离
* leading 空格字符到基准线的距离,为0
* */
Paint.FontMetrics fm = p.getFontMetrics();
c.drawText("Alpha", x/2, (y-fm.ascent)/2, p);
}
public SampleView(Context context) {
super(context);
setFocusable(true);
/*取得资源文件的输入流*/
InputStream is = context.getResources()
.openRawResource(R.drawable.app_sample_code);
/*BitmapFactory 是位图的一个工厂类
* 从各种各样的位图对象中创建位图对象,包括文件,流,字节数组。
* */
mBitmap = BitmapFactory.decodeStream(is);
/*extractAlpha()位图的这个方法是通过提取
* 了原始位图的透明通道值重建新的位图*/
mBitmap2 = mBitmap.extractAlpha();
/*通过位图的宽度和高度已经位图的颜色配置来创建位图
* Bitmap.Config是内部枚举类表示位图的颜色配置
* 它的颜色配置有ALPHA_8、ARGB_4444、ARGB_8888、RGB_565
* */
mBitmap3 = Bitmap.createBitmap(200, 200, Bitmap.Config.ALPHA_8);
drawIntoBitmap(mBitmap3);
/*LinearGradient类是Shader的一个子类,它实现的是一个线性梯度变化的一个
* 着色器,(0,0)到(100,70)的直线式颜色梯度变化线
* 这个梯度变化是在红绿蓝之间均匀变化的
* Shader.TileMode是超出梯度线的颜色变化模式
*CLAMP 固定shader绘画时颜色超过原始边界(梯度线)的部分颜色用边界颜色绘制。
*REPEAT 在水平和垂直方向重复使用着色器的色相,但边界分明
*MIRROR 在水平和垂直方向重复使用着色器的色相,交换的映像色相使得邻
*近的色相总是一致;颜色关于梯度线镜像
* */
mShader = new LinearGradient(0, 0, 100, 70,
new int[] {Color.RED, Color.GREEN, Color.BLUE },
null, Shader.TileMode.MIRROR);
}
@Override protected void onDraw(Canvas canvas) {
canvas.drawColor(Color.WHITE);
Paint p = new Paint();
float y = 10;
/*设置画笔颜色为红色*/
p.setColor(Color.RED);
/*调用画布的drawBitmap方法在指定的位置用指定的画笔画指定的位图*/
canvas.drawBitmap(mBitmap, 10, y, p);
/*设置下一个位图绘制的y坐标值*/
y += mBitmap.getHeight() + 10;
canvas.drawBitmap(mBitmap2, 10, y, p);
y += mBitmap2.getHeight() + 10;
/*设置画笔的着色器*/
p.setShader(mShader);
canvas.drawBitmap(mBitmap3, 10, y, p);
/*这个类主要装载了绘制直线曲线等的几何路径。*/
Path path = new Path();
/*画上面的梯度变化线*/
path.moveTo(0, 0);
path.lineTo(100,70);
p.setColor(Color.RED);
/*Paint.Style画刷的样式枚举类
* STROKE 只绘制笔画形状
* Fill 填充
* FILL_AND_STROKE 既画笔画又填充
* */
p.setStyle(Paint.Style.STROKE);
/*用指定的路径和指定的画刷画要求的路径*/
canvas.drawPath(path, p);
}
}
}
1. Bitmap.extractAlpha()
位图的这个方法是通过提取 了原始位图的透明通道值重建新的位图
2.Bitmap.Config是内部枚举类表示位图的颜色配置, 有ALPHA_8、ARGB_4444、ARGB_8888、RGB_565
3.LinearGradient类是Shader的一个子类,它实现的是一个线性梯度变化的一个着色器,
4.Shader.TileMode是超出梯度线的颜色变化模式
CLAMP--- 固定shader绘画时颜色超过原始边界(梯度线)的部分颜色用边界颜色绘制。REPEAT---在水平和垂直方向重复使用着色器的色相,但边界分明
MIRROR -----在水平和垂直方向重复使用着色器的色相,交换的映像色相使得邻近的色相总是一致;颜色关于梯度线镜像
5.canvas.drawBitmap()
6.Paint.Style画刷的样式枚举类
STROKE--- 只绘制笔画形状
Fill ---填充
FILL_AND_STROKE---既画笔画又填充
7. p.setXfermode(new PorterDuffXfermode(Mode.SRC));
用指定的PorterDuff模型创建xformode,PorterDuff.Mode.SRC表示下面要绘制的文本应在上面绘制的圆的上层
8.Paint.Align 是文本对齐方式的一个枚举类
CENTER---表示文本居中
LEFT ---表示做对齐
RIGHT ---表示右对齐
9.Paint.FontMetrics fm = p.getFontMetrics();
FontMetrics是字体度量的类描述了给定文本大小的各种各样的字体度量。
ascent--- 表示到基准线之上的距离
bottom--表示到基准线之下的最大距离,它是最低的字体类型
descent ---表示到基准线之下的距离
leading--- 空格字符到基准线的距离,为0
10.p.setAntiAlias(true);
在绘制一个新的Paint对象时,可以通过传递给它一些标记来影响它被渲染的方式。ANTI_ALIAS_FLAG是其中一种很有趣的标记,它可以保证在绘制斜线的时候使用抗锯齿效果来平滑该斜线的外观。
在绘制文本的时候,抗锯齿效果尤为重要,因为经过抗锯齿效果处理之后的文本非常容易阅读。要创建更加平滑的文本效果,可以应用SUBPIXEL_TEXT_FLAG,它将会应用子像素抗锯齿效果。也可以手工地使用setSubpixelText和setAntiAlias方法来设置这些标记
[color=red][/color]
1.PathEffect API示例给出了如何应用每一种效果的指导说明。
到目前为止,所有的效应都会影响到Paint填充图像的方式;PathEffect是用来控制绘制轮廓(线条)的方式。
PathEffect对于绘制Path基本图形特别有用,但是它们也可以应用到任何Paint中从而影响线条绘制的方式。
使用PathEffect,可以改变一个形状的边角的外观并且控制轮廓的外表。Android包含了多个PathEffect,包括:
CornerPathEffect 可以使用圆角来代替尖锐的角从而对基本图形的形状尖锐的边角进行平滑。
DashPathEffect 可以使用DashPathEffect来创建一个虚线的轮廓(短横线/小圆点),而不是使用实线。你还可以指定任意的虚/实线段的重复模式。
DiscretePathEffect 与DashPathEffect相似,但是添加了随机性。当绘制它的时候,需要指定每一段的长度和与原始路径的偏离度。
PathDashPathEffect 这种效果可以定义一个新的形状(路径)并将其用作原始路径的轮廓标记。
下面的效果可以在一个Paint中组合使用多个Path Effect。
SumPathEffect 顺序地在一条路径中添加两种效果,这样每一种效果都可以应用到原始路径中,而且两种结果可以结合起来。
ComposePathEffect 将两种效果组合起来应用,先使用第一种效果,然后在这种效果的基础上应用第二种效果。
对象形状的PathEffect的改变会影响到形状的区域。这就能够保证应用到相同形状的填充效果将会绘制到新的边界中。
使用setPathEffect方法可以把PathEffect应用到Paint对象中,如下所示:
paint.setPathEffect(new CornerPathEffect(5));
frame-by-frame animation 是一个展示一连串图片的简单的动画
实现frame-by-frame animation 的步骤
A将一连串图片存放在drawable文件夹中
B define the list of frames to be animated. the name of the file is aframe_animation.xml
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
<item android:drawable="@drawable/circle1" android:duration="50" />
<item android:drawable="@drawable/circle2" android:duration="50" />
<item android:drawable="@drawable/circle3" android:duration="50" />
</animation-list>
上面这个xml的作用时将这三张图片一次放映,时间间隔是50mill second
create XML Layout File for the Frame Animation Example
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id = "@+id/textViewId1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Debuge Scrath pad"
/>
<Button
android:id = "@+id/startButtonId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="start animation"/>
<ImageView
android:id = "@+id/animationImage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
the main Activity
package hust.ophoneclub.FrameAnimation;
import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class FrameAnimation extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setupButton();
}
/**
*
*/
private void setupButton() {
Button btn =(Button)findViewById(R.id.startButtonId);
btn.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v) {
startAnimate();
}
});
}
private void startAnimate() {
ImageView imageView = setBackgroundOfImageView();
animate(imageView);
}
/**
* @param imageView
*/
private void animate(ImageView imageView) {
AnimationDrawable frameAnimation =
(AnimationDrawable)imageView.getBackground();
if(frameAnimation.isRunning()){
frameAnimation.stop();
}else{
frameAnimation.stop();
frameAnimation.start();
}
}
private ImageView setBackgroundOfImageView() {
ImageView imageView =(ImageView)findViewById(R.id.animationImage);
imageView.setVisibility(ImageView.VISIBLE);
imageView.setBackgroundResource(R.drawable.frame_animation);
return imageView;
}
}
说明:frameAnimation是作为main.xml中的background
imageView.setBackgroundResource(R.drawable.frame_animation);
这句代码的作用是将定义frame Animation 的 xml rescourse 文件转化为一个AnimationDrwable 对象
加到imageView的背景上 所以我们要取得这个对象来进行相关的操作
AnimationDrawable frameAnimation =
(AnimationDrawable)imageView.getBackground();
这句代码就取得了这个对象;
最后的效果是这样的