`
Weich_JavaDeveloper
  • 浏览: 97801 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

ImageView 自适应手机屏幕 + 旋转

 
阅读更多

Chart.java

public class Chart extends VLinearLayout implements BaseView{

	private Activity context;

	private ImageView m_ImageView;

	private Bitmap img;

	private int ScaleAngle = 0; 

	private Button turnLeft,turnRight;

	private HLinearLayout B_LinearLayout;

	private int[] WindowXY = new int[2];

	public Chart(Activity context) {
		super(context);
		this.context = context;
		m_ImageView = new ImageView(context);
		DisplayMetrics dm = new DisplayMetrics();
		context.getWindowManager().getDefaultDisplay().getMetrics(dm);
		WindowXY[0] = dm.widthPixels;
		WindowXY[1] = dm.heightPixels;
		B_LinearLayout = new HLinearLayout(context);
		turnLeft = new Button(context);
		turnLeft.setText("左转");
		turnRight = new Button(context);
		turnRight.setText("右转");
		turnLeft.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {turnLeft();}
		});
		turnRight.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {turnRight();}
		});
		B_LinearLayout.addView(turnLeft);
		turnLeft.setPadding(10, 5, 10, 5);
		B_LinearLayout.addView(turnRight);
		turnRight.setPadding(10, 5, 10, 5);
		this.addView(B_LinearLayout);
		this.addView(m_ImageView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));
	}

	public void turnLeft(){

		ScaleAngle--; 
		int bmpWidth = img.getWidth();
		int bmpHeight = img.getHeight();
		Matrix matrix = new Matrix(); 
		matrix.setRotate( 90 * ScaleAngle); 
		Bitmap newBit = Bitmap.createBitmap(img, 0, 0, bmpWidth, bmpHeight, matrix, true);
		m_ImageView.setImageBitmap(newBit);
	}

	public void turnRight(){

		ScaleAngle++; 
		int bmpWidth = img.getWidth();
		int bmpHeight = img.getHeight(); 
		Matrix matrix = new Matrix(); 
		matrix.setRotate( 90 * ScaleAngle); 
		Bitmap newBit = Bitmap.createBitmap(img, 0, 0, bmpWidth, bmpHeight, matrix, true);
		m_ImageView.setImageBitmap(newBit);
	}

	public void setImage(String param){
		if (param == null || param.equals("")){
			Log.v("default", "image");
			try {
				img = BitmapFactory.decodeStream(context.getResources().getAssets().open("test.png"));
			} catch (IOException e) {
				Log.e("读取Assets下图片资源失败!", e.getMessage(), e);
			}
		}else{

			if (param.startsWith("/")) {
				img = BitmapFactory.decodeStream(ImageUtil.class.getResourceAsStream(param));
			} else if (param.startsWith("res-img:")) {
				try {
					img = BitmapFactory.decodeStream(context.getResources().getAssets().open(param.substring(8)));
				} catch (IOException e) {
					Log.e("读取Assets下图片资源失败!", e.getMessage(), e);
				}
			} else if (param.startsWith("stream:")) {
				String s = param.substring(7);
				img = BitmapFactory.decodeStream(new java.io.ByteArrayInputStream(StringUtil.fromBase64(s)));
			}
		}
		
		int bmpWidth = img.getWidth();
		int bmpHeight = img.getHeight();
		Log.v("base", bmpWidth+"");
		Log.v("base", bmpHeight+"");
		if(bmpWidth < WindowXY[0] && bmpHeight < WindowXY[1]){
			Matrix matrix = new Matrix(); 
			float one = (float)WindowXY[0]/bmpWidth;
			float two = (float)WindowXY[1]/bmpHeight;
			Log.v("one", one+"");
			Log.v("two", two+"");
			if(one < two){
				Log.v("one", one+"");
				matrix.postScale(one, one);
			}else{
				Log.v("two", two+"");
				matrix.postScale(two, two);
			}
			img = Bitmap.createBitmap(img, 0, 0, bmpWidth, bmpHeight, matrix, true);
		}
		m_ImageView.setImageBitmap(img);

	}

	@Override
	public String getValue() {
		return null;
	}

	@Override
	public void setValue(String text) {

	}

	@Override
	public View getView() {
		return this;
	}
}
 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics