Canvas的save()和restore()以及clipRect()方法测试

MainActivity如下:

package cn.testcanvas;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
/**
 * Demo描述:
 * canvas的save()和restore()以及clipRect()方法测试
 *
 * Demo总结:
 * 1 save()和restore()要配对使用(restore可以比save少,但不能多)
 *   若restore调用次数比save多,会报错
 * 2 canvas剪裁后绘制的东西只能在裁剪区域的范围能才能显示出来
 *
 * 参考资料:
 * http://blog.csdn.net/lonelyroamer/article/details/8264189
 *
 *
 */
public class MainActivity extends Activity {
    private ImageView mImageView;
    private ImageView mClipedImageView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}
    private void init(){
    	mImageView=(ImageView) findViewById(R.id.imageView);
    	mClipedImageView=(ImageView) findViewById(R.id.clipedImageView);
    	//分开测试
    	//testClip();
    	testSaveAndRestore();
    }

    private void testClip(){
    	Bitmap bitmap=Bitmap.createBitmap(200, 200, Config.ARGB_8888);
    	Canvas canvas=new Canvas(bitmap);
    	canvas.drawColor(Color.GREEN);
    	Paint paint=new Paint();
    	paint.setColor(Color.YELLOW);
    	canvas.drawText("绿色部分为Canvas剪切前的区域", 20, 50, paint);

    	Rect rect=new Rect(10,95,180,140);
    	canvas.clipRect(rect);
    	canvas.drawColor(Color.YELLOW);
    	paint.setColor(Color.BLACK);
    	//canvas剪裁后绘制的东西只能在裁剪区域的范围能才能显示出来
    	canvas.drawText("黄色部分为Canvas剪切后的区域", 10, 110, paint);
    	mImageView.setImageBitmap(bitmap);

    	//显示截取后的Bitmap:
    	Bitmap newBitmap=Bitmap.createBitmap(bitmap,10,95,170,45);
    	mClipedImageView.setImageBitmap(newBitmap);

    }

    private void testSaveAndRestore(){
    	Bitmap bitmap=Bitmap.createBitmap(200, 200, Config.ARGB_8888);
    	Canvas canvas=new Canvas(bitmap);
    	Paint paint=new Paint();
    	paint.setColor(Color.RED);
    	paint.setTextSize(25);
    	canvas.rotate(15);
    	canvas.drawText("Hello Android 1", 20, 30, paint);
    	canvas.translate(50, 50);
    	//注意:save()方法
    	//保存在此save()方法之前的canvas的操作
    	//比如:roate(),translate(),clipXXX()
    	canvas.save();

    	paint.setColor(Color.GREEN);
    	paint.setTextSize(15);
    	canvas.rotate(60);
    	canvas.translate(-20,-20);
    	canvas.drawText("Hello Android 2", 20, 60, paint);

    	//注意:restore()方法将save()方法之
    	//后Canvas的roate(),translate(),clipXXX()的操作清空
    	canvas.restore();
    	paint.setColor(Color.BLACK);
    	paint.setTextSize(15);
    	canvas.drawText("Hello Android 3", 20, 70, paint);

    	mImageView.setImageBitmap(bitmap);

    }
}

main.xml如下:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="测试Canvas"
        android:layout_centerHorizontal="true"
    />
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textView"
        android:layout_centerInParent="true"
     />
    <ImageView
        android:id="@+id/clipedImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/imageView"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="15dip"
     />

</RelativeLayout>

 

时间: 2024-09-28 05:48:27

Canvas的save()和restore()以及clipRect()方法测试的相关文章

Android canvas画图操作之切割画布实现方法(clipRect)_Android

本文实例讲述了Android canvas画图操作之切割画布实现方法.分享给大家供大家参考,具体如下: android切割画布的历程不算很难,可是理解起来也比较麻烦,这里写一下我的理解 但是不一定正确: canvas.clipRect(30, 30, 70, 70, Region.Op.XOR); 最后一个参数有多个选择分别是: //DIFFERENCE是第一次不同于第二次的部分显示出来 //REPLACE是显示第二次的 //REVERSE_DIFFERENCE 是第二次不同于第一次的部分显示

Android canvas画图操作之切割画布实现方法(clipRect)

本文实例讲述了Android canvas画图操作之切割画布实现方法.分享给大家供大家参考,具体如下: android切割画布的历程不算很难,可是理解起来也比较麻烦,这里写一下我的理解 但是不一定正确: canvas.clipRect(30, 30, 70, 70, Region.Op.XOR); 最后一个参数有多个选择分别是: //DIFFERENCE是第一次不同于第二次的部分显示出来 //REPLACE是显示第二次的 //REVERSE_DIFFERENCE 是第二次不同于第一次的部分显示

Android ApiDemos示例解析(14) App-&amp;gt;Activity-&amp;gt;Save &amp;amp; Restore State

Save & Restore State与之前的例子Android ApiDemo示例解析(9):App->Activity->Persistent State 实现的 UI类似,但功能和实现方法稍有不同.(9)是通过Shared Preferences 和 Activity 的onPause() ,和onResume()来保持UI中 EditText 的值.本例是通过onSaveInstanceState(Bundle savedBundle) 来实现保持UI状态. 和onPause

html5 canvas-HTML5 canvas,画图时有什么属性或者方法可以看到画图的路径过程

问题描述 HTML5 canvas,画图时有什么属性或者方法可以看到画图的路径过程 HTML5 canvas,画图时有什么属性或者方法可以看到画图的路径过程?画一个矩形,要看到矩形是怎么画出来的,一个过程动画,谢谢大大给我解答 解决方案 Html5 Canvas画图html5-canvas画图html5 canvas画图

两种方法测试spring中的jdbc

两种方法测试spring中的jdbc  JDBC是一个非常基础的数据存取API,spring对其进行简单的封装,  下面以sqlserver中自带的pubs数据库Authors表进行测试.   1):编写Authors.java,其每个对象对应于数据库中的一条记录   package jdbc;public class Authors {   String  lname=null;   String fname=null;   String phone=null;   String addres

junit-ECLIPSE现在使用JUNIT4测试,只有方法测试失败的时候才会显示左侧的测试方法列表么?

问题描述 ECLIPSE现在使用JUNIT4测试,只有方法测试失败的时候才会显示左侧的测试方法列表么? ECLIPSE现在使用JUNIT4测试,只有方法测试失败的时候才会显示左侧的测试方法列表么? 解决方案 参考下:http://www.tuicool.com/articles/fArMFjJ 解决方案二: 参考下:http://www.tuicool.com/articles/fArMFjJ

validate-laravel中phpunit使用call方法测试controller无法通过integer验证

问题描述 laravel中phpunit使用call方法测试controller无法通过integer验证 我在本地用浏览器或者curl方式访问http://api.lizhongde1.dev.anhouse.com.cn/hft/1.0/zf/onlinelist?page_size=10地址时,都能够正常得到数据,但是在服务器中使用laravel自带的call方法验证就一直提示integer验证错误,到底怎么才能通过验证呢?我的test方法: public function testOnL

junit-Android中activity的方法测试

问题描述 Android中activity的方法测试 比如对于一个acticity,其中有一个方法a();我该怎么用junit框架对其进行测试呢? 解决方案 我是用爱内测http://t.cn/R2gpSpW 做检测的耶...因为我觉得还是挺方便快捷的 解决方案二: 建一个Java类继承TestCase类,把要测试的方法放进来,方法上加上注解,(如@UiThreadTest)具体哪个注解查下,然后在清单文件配置, android:targetPackage="com.example.androi

TestNG方法测试及注意要点 代码及配置详解(解决testng方法不执行问题)

教你解决为什么TestNG中方法加了@Test注解,也在配置文件中配置了,但是方法就是不执行! 在使用TestNG进行测试时,使用配置文件的方式更容易于维护,但是经常遇到明明方法写了也配置执行了,但是run的时候代码就没有执行 看代码:(仔细看注释!) /** * * <p> * Title: TestngMethods * </p> * * <p> * 对应配置文件testng-methods.xml * Description: Testng的methods测试及配