SurfaceView繪制總結(加載大圖縮放比例)


package com.mao.maodemo;

import com.mao.maodemo.base.ActivityBase;

import android.annotation.SuppressLint;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.Rect;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnTouchListener;
/**
* 測試OpenGLDemo
*
* Render類 主要功能為對SurfaceView進行渲染。
* 有關SurfaceView的相關知識點 1、SurfaceView與SurfaceHolder一般綁定使用。 SurfaceHolder=SurfaceView.getHolder();
* SurfaceHolder.lockCanvas() 對畫布鎖定,獲取SurfaceView的Canvas
* SurfaceHolder.lockCanvas(Rect rect) 鎖定某一片區域,獲取到區域的畫布
* SurfaceHolder.unLocalCanvasAndPost(Canvas canvas) 對繪制畫布進行釋放資源。但是當圖形還在緩沖的過程中重新調用Lock會造成當前的圖像覆蓋掉
* 主要測試功能如下:
* 1、構建OpenGL的相關方法以及測試實現OpenGL主要功能與方法
* 2、利用OpenGL結合SurfaceView繪制頁面效果 也可以直接使用GLSurfaceView進行繪制
*
*
*
*
*
*
*
*
*
*
*
* @author Ming
* @date 2014 上午9:38:27
*/
@SuppressLint("ClickableViewAccessibility")
public class ActivityOpenGLTest extends ActivityBase implements SurfaceHolder.Callback,OnTouchListener{

public static final String TAG="MaoDemo---OpenGL---";
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private Paint mPaint;
private Point mPointss;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_open_gltest);
mPointss=new Point();
getWindowManager().getDefaultDisplay().getSize(mPointss);
init();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_open_gltest, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

private void init(){
mSurfaceView=(SurfaceView) findViewById(R.id.main_surfaceView_NoGL);
mPaint=new Paint();
mPaint.setAntiAlias(true);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setColor(getResources().getColor(R.color.red));
mPaint.setStrokeWidth(2);
mSurfaceHolder=mSurfaceView.getHolder();
mSurfaceHolder.addCallback(this);
mSurfaceView.setOnTouchListener(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {

Canvas mCanvas=mSurfaceHolder.lockCanvas();

Bitmap mBitmap=decodeSampledBitmapFromResource(getResources(), R.drawable.star_bg_img, mPointss.x, mPointss.y);

mCanvas.drawBitmap(mBitmap, 0, 0, mPaint);

mSurfaceHolder.unlockCanvasAndPost(mCanvas);
//重新鎖定持久化上次繪制的內容
mSurfaceHolder.lockCanvas(new Rect(0,0,0,0));

mSurfaceHolder.unlockCanvasAndPost(mCanvas);

}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
Log.d(TAG, "surfaceChanged");
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
Log.d(TAG, "surfaceDestroyed");
}

@Override
public boolean onTouch(View v, MotionEvent event) {
int actionMask=event.getAction() & MotionEvent.ACTION_MASK;
if(actionMask==MotionEvent.ACTION_DOWN){
int point_x=(int) event.getX();
int point_y=(int) event.getY();
Log.d(TAG+"point_x", ""+point_x);
Log.d(TAG+"point_y", ""+point_y);
//獲取鎖定區域,只更新指定區域內容
//調用SurfaceView進行繪制頁面,依次會覆蓋上一個繪制的頁面。其余的互不影響。
// *********New Rect() 里面的參數應該嚴格控制為Left Top Right Buttom 否則造成不顯示***********
Canvas mCanvas=mSurfaceHolder.lockCanvas(new Rect(point_x-100,point_y-100,point_x+100,point_y+100));

mCanvas.drawCircle(point_x, point_y, 20, mPaint);

//繪制完成之后提交
mSurfaceHolder.unlockCanvasAndPost(mCanvas);

}
return false;
}

@Override
protected void onDestroy() {
mSurfaceHolder=null;
mSurfaceView=null;
super.onDestroy();
}
/**
* 計算當前圖片的縮放比率
* @param options
* @param reqWidth
* @param reqHeight
* @return
*/
public static int calculateInSampleSize(BitmapFactory.Options options,int reqWidth, int reqHeight) {
//獲取加載圖片信息
final int height = options.outHeight;
final int width = options.outWidth;
//返回正常大小的圖片
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

//分別獲取在高度以及寬度上的不同縮放比率。選擇一個較小的縮放比率進行縮放
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);

inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
/**
* 對圖片進行解碼
* @param res
* @param resId
* @param reqWidth
* @param reqHeight
* @return
*/
public static Bitmap decodeSampledBitmapFromResource(Resources res,int resId, int reqWidth, int reqHeight) {

//final 修飾對象只是對應的引用地址不會發生改變,但是對象的屬性內容都是可以修改
final BitmapFactory.Options options = new BitmapFactory.Options();
//設置加載圖片的模式。有效防止加載大圖造成OEM
options.inJustDecodeBounds = true;

BitmapFactory.decodeResource(res, resId, options);

// 計算返回圖片大小的縮放比率 例如當options.inSampleSize=4時意味着返回的圖片的大小只有1/16 有效減小內存的使用
<span style="white-space:pre">		</span>/**<span style="white-space:pre">		</span>  * inSampleSize == 4 returns                  * an image that is 1/4 the width/height of the original, and 1/16 the                  * number of pixels.<span style="white-space:pre">		</span>  */		options.inSampleSize = calculateInSampleSize(options, reqWidth,reqHeight);		//設置加載模式為真正加載圖片		options.inJustDecodeBounds = false;				return BitmapFactory.decodeResource(res, resId, options);	}}


注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
粤ICP备14056181号  © 2014-2020 ITdaan.com