package com.pingyijinren.helloworld.activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.widget.ImageView; import com.pingyijinren.helloworld.R; public class MainActivity extends AppCompatActivity { private ImageView imageView; private Bitmap bitmap; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView=(ImageView)findViewById(R.id.imageView); bitmap=zoomBitmap(2,2); imageView.setImageBitmap(bitmap); } private Bitmap zoomBitmap(int width,int height) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; options.inPurgeable=true; options.inInputShareable=true; bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.beauty, options); float scaleWidth = (float) 1.0 / width; float scaleHeight = (float) 1.0 / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap scaleBitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); bitmap.recycle(); return scaleBitmap; } }
时间: 2024-10-31 09:51:17