用android代码显示图片的一部分源码

时间:2023-01-24 05:12:54

ShowPoritionPictureActivity代码:

[java] <span style="font-size:16px;">

package com.iwin.zzs;

import android.app.Activity; 

import android.graphics.Bitmap; 

import android.graphics.BitmapFactory; 

import android.graphics.Matrix; 

import android.os.Bundle; 

import android.util.DisplayMetrics; 

import android.view.WindowManager; 

 

public class ShowPoritionPictureActivity extends Activity { 

    /** Called when the activity is first created. */ 

    Bitmap picRes; 

     

    Bitmap showPic; 

     

    //获取原图片的宽和高  

    int picWidth; 

    int picHeight; 

     

    private PoritionView poritonView = null; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); 

         

        // 不显示状态栏  

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN); 

        DisplayMetrics dm = new DisplayMetrics(); 

        this.getWindowManager().getDefaultDisplay().getMetrics(dm); 

         

        // 得到屏幕的长和宽  

        int screenWidth = dm.widthPixels;                //水平分辨率  

        int screenHeight = dm.heightPixels;              //垂直分辨率  

         

         

        picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl); 

        // 得到图片的长和宽  

        picWidth = picRes.getWidth(); 

        picHeight = picRes.getHeight(); 

         

        // 计算缩放率,新尺寸除原始尺寸  

        float scaleWidth = ((float) screenWidth ) / picWidth; 

        float scaleHeight = ((float) screenHeight ) / picHeight; 

         

        // 创建操作图片用的matrix对象  

        Matrix matrix = new Matrix(); 

        // 缩放图片动作  

        matrix.postScale(scaleWidth, scaleHeight); 

        // 新得到的图片是原图片经过变换填充到整个屏幕的图片  

        Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true); 

        // bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);  

        // canvas=new Canvas();           

        // canvas.setBitmap(bitmap);   

         

        showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100); 

         

        poritonView = new PoritionView(this); 

        poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50); 

        setContentView(poritonView); 

    } 

</span> 

<span style="font-size:16px;">package com.iwin.zzs;

import android.app.Activity;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

import android.graphics.Matrix;

import android.os.Bundle;

import android.util.DisplayMetrics;

import android.view.WindowManager;

public class ShowPoritionPictureActivity extends Activity {

    /** Called when the activity is first created. */

 Bitmap picRes;

 

 Bitmap showPic;

 

 //获取原图片的宽和高

    int picWidth;

    int picHeight;

   

    private PoritionView poritonView = null;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

       

        // 不显示状态栏

        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);

        DisplayMetrics dm = new DisplayMetrics();

  this.getWindowManager().getDefaultDisplay().getMetrics(dm);

  

  // 得到屏幕的长和宽

  int screenWidth = dm.widthPixels;                //水平分辨率

  int screenHeight = dm.heightPixels;              //垂直分辨率

  

  

        picRes = BitmapFactory.decodeResource(this.getResources(), R.drawable.girl);

        // 得到图片的长和宽 www.2cto.com

        picWidth = picRes.getWidth();

     picHeight = picRes.getHeight();

       

     // 计算缩放率,新尺寸除原始尺寸

     float scaleWidth = ((float) screenWidth ) / picWidth;

     float scaleHeight = ((float) screenHeight ) / picHeight;

  

     // 创建操作图片用的matrix对象

     Matrix matrix = new Matrix();

     // 缩放图片动作

     matrix.postScale(scaleWidth, scaleHeight);

     // 新得到的图片是原图片经过变换填充到整个屏幕的图片

     Bitmap picNewRes = Bitmap.createBitmap(picRes, 0, 0,picWidth, picHeight, matrix, true);

     // bitmap = Bitmap.createBitmap(400, 480, Bitmap.Config.ARGB_8888);

  // canvas=new Canvas();        

  // canvas.setBitmap(bitmap);

  

     showPic = Bitmap.createBitmap(picNewRes, screenWidth/2-50, screenHeight/2-50, 100, 100);

    

     poritonView = new PoritionView(this);

     poritonView.setBitmapShow(showPic, screenWidth/2-50, screenHeight/2-50);

     setContentView(poritonView);

    }

</span>

PoritionView类代码:

[java] <span style="font-size:16px;">package com.iwin.zzs; 

 

import android.content.Context; 

import android.graphics.Bitmap; 

import android.graphics.Canvas; 

import android.view.View; 

 

public class PoritionView extends View { 

    private Bitmap showPic = null; 

    private int startX = 0; 

    private int startY = 0; 

    public PoritionView(Context context) { 

        super(context); 

        // TODO Auto-generated constructor stub  

    } 

 

    @Override 

    protected void onDraw(Canvas canvas) { 

        // TODO Auto-generated method stub  

        super.onDraw(canvas); 

        canvas.drawBitmap(showPic, startX, startY, null); 

    } 

    public void setBitmapShow(Bitmap b, int x, int y) 

    { 

        showPic = b; 

        startX = x; 

        startY = y; 

    } 



</span> 

<span style="font-size:16px;">package com.iwin.zzs;

import android.content.Context;

import android.graphics.Bitmap;

import android.graphics.Canvas;

import android.view.View;

public class PoritionView extends View {

 private Bitmap showPic = null;

 private int startX = 0;

 private int startY = 0;

 public PoritionView(Context context) {

  super(context);

  // TODO Auto-generated constructor stub

 }

@Override

 protected void onDraw(Canvas canvas) {

  // TODO Auto-generated method stub

  super.onDraw(canvas);

  canvas.drawBitmap(showPic, startX, startY, null);

 }

 public void setBitmapShow(Bitmap b, int x, int y)

 {

  showPic = b;

  startX = x;

  startY = y;

 }

}

</span>

在工程res/drawable里面添加图片gir.png,运行程序的效果是只显示图片的中间100*100的那部分图片。