【1.最终效果】
【2.xml文件】
【3.view文件】
package com.example.tupian2;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
public class MyView extends View {
Paint paint;
Bitmap bitmap;
public MyView(Context context, AttributeSet attrs) {
super(context, attrs);
this.initBitmap();
}
public void initBitmap() {
paint = new Paint();
bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.img);
}
/*
* 调用setConcat方法,将对应的两个Matrix对象连接起来并绘制,最后恢复画布状态
* */
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setAntiAlias(true);
paint.setColor(Color.BLUE);
paint.setTextSize(15);
canvas.drawBitmap(bitmap, 10, 10, paint); // 第1个图
canvas.save();
/*
* 调用setTranslate方法平移矩阵,setScale方法缩放矩阵,setCRotate方法旋转
* */
Matrix m1 = new Matrix();
m1.setTranslate(1000, 100);
Matrix m2 = new Matrix();
m2.setRotate(30);
Matrix m3 = new Matrix();
m3.setConcat(m1, m2);
m1.setScale(0.8f, 0.8f);
m2.setConcat(m3, m1);
canvas.drawBitmap(bitmap, m2, paint); // 第2个图
canvas.restore();
canvas.save();
paint.setAlpha(180);
m1.setTranslate(200, 300);
m2.setScale(1.3f, 1.3f);
m3.setConcat(m1, m2);
canvas.drawBitmap(bitmap, m3, paint);// 第3个图
paint.reset(); // 恢复画笔设置
canvas.restore();// 恢复画布
paint.setTextSize(40);
paint.setColor(Color.RED);
canvas.drawText("图片高度:" + bitmap.getHeight(), 100, 800, paint);
canvas.drawText("图片宽度:" + bitmap.getWidth(), 100, 900, paint);
paint.reset();
}
}
【4.Activity文件】
package com.example.tupian2;
import android.os.Bundle;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.view.Menu;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/* 去掉名称 。没有这一行的效果如图:(2)*/
requestWindowFeature(Window.FEATURE_NO_TITLE);
/* 去掉电量显示这些。没有这一行,效果如图:(3)*/
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
/* 设置横屏。没有这一行,效果如图:(4)*/
/* setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);*/
setContentView(R.layout.activity_main);
}
}
【4.过程错误:】
03-25 20:19:20.348: E/AndroidRuntime(4235): FATAL EXCEPTION: main
03-25 20:19:20.348: E/AndroidRuntime(4235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tupian2/com.example.tupian2.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.MyView
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2383)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.access$600(ActivityThread.java:156)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.os.Looper.loop(Looper.java:153)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.main(ActivityThread.java:5331)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
03-25 20:19:20.348: E/AndroidRuntime(4235): at dalvik.system.NativeStart.main(Native Method)
03-25 20:19:20.348: E/AndroidRuntime(4235): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.MyView
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Activity.setContentView(Activity.java:1943)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.example.tupian2.MainActivity.onCreate(MainActivity.java:17)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Activity.performCreate(Activity.java:5191)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1091)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
03-25 20:19:20.348: E/AndroidRuntime(4235): ... 11 more
03-25 20:19:20.348: E/AndroidRuntime(4235): Caused by: java.lang.ClassNotFoundException: Didn't 03-25 20:19:20.348: E/AndroidRuntime(4235): FATAL EXCEPTION: main
03-25 20:19:20.348: E/AndroidRuntime(4235): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tupian2/com.example.tupian2.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.MyView
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2319)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2383)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.access$600(ActivityThread.java:156)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1340)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.os.Handler.dispatchMessage(Handler.java:99)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.os.Looper.loop(Looper.java:153)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.main(ActivityThread.java:5331)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.reflect.Method.invokeNative(Native Method)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.reflect.Method.invoke(Method.java:511)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
03-25 20:19:20.348: E/AndroidRuntime(4235): at dalvik.system.NativeStart.main(Native Method)
03-25 20:19:20.348: E/AndroidRuntime(4235): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.example.MyView
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:274)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Activity.setContentView(Activity.java:1943)
03-25 20:19:20.348: E/AndroidRuntime(4235): at com.example.tupian2.MainActivity.onCreate(MainActivity.java:17)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Activity.performCreate(Activity.java:5191)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1091)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2283)
03-25 20:19:20.348: E/AndroidRuntime(4235): ... 11 more
03-25 20:19:20.348: E/AndroidRuntime(4235): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.MyView" on path: DexPathList[dexElements=[zip file "/data/app/com.example.tupian2-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.tupian2-2, /vendor/lib, /system/lib]]
03-25 20:19:20.348: E/AndroidRuntime(4235): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-25 20:19:20.348: E/AndroidRuntime(4235): ... 21 more
find class "com.example.MyView" on path: DexPathList[dexElements=[zip file "/data/app/com.example.tupian2-2.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.tupian2-2, /vendor/lib, /system/lib]]
03-25 20:19:20.348: E/AndroidRuntime(4235): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-25 20:19:20.348: E/AndroidRuntime(4235): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createView(LayoutInflater.java:552)
03-25 20:19:20.348: E/AndroidRuntime(4235): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:687)
03-25 20:19:20.348: E/AndroidRuntime(4235): ... 21 more
找不到com.example.MyView类,改成com.example.tupian.MyView后,,程序没有错误了