<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/frameLayout1" android:orientation="vertical" > <ImageView android:id="@+id/imageView1" android:layout_width="100px" android:paddingTop="5px" android:layout_height="25px"/> </FrameLayout>
package com.example.test; import java.io.File; import android.app.Activity; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.os.Bundle; import android.os.Environment; import android.view.View; import android.widget.FrameLayout; import android.widget.ImageView; public class MainActivity extends Activity { private ImageView iv;//声明一个ImageView组件的对象 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //获取布局文件中添加的ImageView组价 iv=(ImageView)findViewById(R.id.imageView1); //获取布局文件中添加的帧布局管理器 FrameLayout ll=(FrameLayout)findViewById(R.id.frameLayout1); //将自定义的MyView视图添加到帧布局 ll.addView(new MyView(this)); } public class MyView extends View{ public MyView(Context context) { super(context); // TODO Auto-generated constructor stub } @Override protected void onDraw(Canvas canvas) { Paint paint=new Paint(); String path=getSDPath() +"/Pictures/img1.jpg"; Bitmap bitmap=BitmapFactory.decodeFile(path); canvas.drawBitmap(bitmap, 0, 30, paint); Rect src=new Rect(0,0,100,100); Rect dst=new Rect(420,30,500,120); canvas.drawBitmap(bitmap, src, dst, paint); Bitmap bm=Bitmap.createBitmap(new int[]{Color.RED,Color.GREEN,Color.BLUE, Color.MAGENTA},4,1,Config.RGB_565);//使用颜色数组创建一个bitmap对象 iv.setImageBitmap(bm);//为ImageView指定要显示的位图 super.onDraw(canvas); } } /*用来动态获取内存卡根目录的方法*/ public String getSDPath(){ File sdDir = null; boolean sdCardExist = Environment.getExternalStorageState() .equals(android.os.Environment.MEDIA_MOUNTED); //判断sd卡是否存在 if (sdCardExist) //如果SD卡存在,则获取跟目录 { sdDir = Environment.getExternalStorageDirectory();//获取跟目录 } return sdDir.toString(); } }
运行效果如图所示
转载请注明出处:http://blog.csdn.net/acmman/article/details/45458451