转自:http://www.cnblogs.com/renhui/p/7456956.html
1. ImageView 绘制图片
package com.ddx.testimageview; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.widget.ImageView; import java.io.File; public class ShowImageView extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_image_view); //Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg"); Bitmap bitmap = BitmapFactory.decodeFile(Environment.getDataDirectory().getPath() + File.separator + "11.jpg"); Log.d("DDX", "externalStorageDirectory = " + Environment.getExternalStorageDirectory().getPath() + File.separator + "11.jpg"); ImageView imageView = (ImageView) findViewById(R.id.imageView); imageView.setImageBitmap(bitmap); } }
2. SurfaceView绘制图片
package com.ddx.testimageview; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.os.Environment; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.widget.ImageView; import java.io.File; public class ShowImageView extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_show_image_view); SurfaceView surfaceView = (SurfaceView) findViewById(R.id.surfaceView); surfaceView.getHolder().addCallback(new SurfaceHolder.Callback() { @Override public void surfaceCreated(SurfaceHolder surfaceHolder) { if(surfaceHolder == null) return; Paint paint = new Paint(); paint.setAntiAlias(true); paint.setStyle(Paint.Style.STROKE); Bitmap bitmap = BitmapFactory.decodeFile(Environment.getDataDirectory().getPath() + File.separator + "11.jpg");//获取Bitmap Canvas canvas = surfaceHolder.lockCanvas();//先锁定当前surfaceView的画面 canvas.drawBitmap(bitmap, 0, 0, paint);//执行绘制操作 surfaceHolder.unlockCanvasAndPost(canvas);//解决锁定并显示在界面上 } @Override public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) { } @Override public void surfaceDestroyed(SurfaceHolder surfaceHolder) { } }); } }
3. 自定义View绘制图片
https://blog.csdn.net/iwanghang/article/details/54018759
package com.ddx.customview; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.os.Environment; import android.util.AttributeSet; import android.view.View; import java.io.File; /** * Created by dingdx on 2018/7/19. */ public class NewView extends View { Paint mPaint = new Paint(); Bitmap mBitmap; public NewView(Context context) { super(context); mPaint.setAntiAlias(true); mPaint.setStyle(Paint.Style.STROKE); mBitmap = BitmapFactory.decodeFile(Environment.getDataDirectory().getPath() + File.separator + "11.jpg"); } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); // 不建议在onDraw做任何分配内存的操作 if(mBitmap != null) { canvas.drawBitmap(mBitmap,0,0,mPaint); } /*mPaint = new Paint(Paint.ANTI_ALIAS_FLAG); //mPaint.setColor(Color.BLUE); mPaint .setColor(Color.argb(60, 0, 0, 0)); canvas.drawCircle(300,300,200,mPaint);*/ } }
注:如果是getExternalStorageDirectory别忘记了权限,否则是不会展示成功的。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>