郑州app开发android加载大图实例
MainActivity.java
package cn.xhhkj.image;
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "结果是";
private static final int UPDATE_PROGRESS = 0;
public String path = "mnt/sdcard/Music/1.jpg";
private ImageView iv_image;
private int screenWidth;
private int screenHeight;
Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
iv_image = findViewById(R.id.iv_pic);
screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
Point outSize = new Point();
getWindowManager().getDefaultDisplay().getSize(outSize);
Log.d(TAG,"宽度"+outSize.x+"高度"+outSize.y);
}
public void loadpic(View view) {
Bitmap bitmap = BitmapFactory.decodeFile(path);
iv_image.setImageBitmap(bitmap);
//loadpic2();
}
public void loadpic2(){
BitmapFactory.Options option =new BitmapFactory.Options();
option.inJustDecodeBounds = true;
int width = option.outWidth;
int height = option.outHeight;
if(width>screenWidth||height>screenHeight){
int widthIndex = Math.round((float)width/(float)screenWidth);
int heightIndex = Math.round((float)height/(float)screenHeight);
option.inSampleSize = Math.max(widthIndex, heightIndex);
}
option.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFile(path, option);
iv_image.setImageBitmap(bitmap);
}
public void loadpic3(){
BitmapFactory.Options option =new BitmapFactory.Options();
option.inSampleSize = 1;
Bitmap bitmap = null;
int i = 1;
for(;;){
try {
option.inSampleSize = i;
bitmap = BitmapFactory.decodeFile(path, option);
break;
} catch (Error e) {
i*=2;
System.out.println("i = "+i);
}
}
iv_image.setImageBitmap(bitmap);
}
}