我如何能显示图片的全尺寸? [英] How can I show the full size of Image?
本文介绍了我如何能显示图片的全尺寸?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的,新的编程机器人。我发现这个例子(下面的例子)在互联网网站,这是一个伟大的教程!当我一旦点击图片上的GridView的我想显示图像的全尺寸什么我想要实现的。
公共类MainActivity延伸活动{
公共类ImageAdapter扩展了BaseAdapter {
私人语境mContext;
ArrayList的<字符串> ITEMLIST =新的ArrayList<字符串>();
公共ImageAdapter(上下文C){
mContext = C;
}
空加(字符串路径){
itemList.add(路径);
}
@覆盖
公众诠释getCount将(){
返回itemList.size();
}
@覆盖
公共对象的getItem(INT为arg0){
// TODO自动生成方法存根
返回null;
}
@覆盖
众长getItemId(INT位置){
// TODO自动生成方法存根
返回0;
}
@覆盖
公共查看getView(INT位置,查看convertView,ViewGroup中父){
ImageView的ImageView的;
如果(convertView == NULL){//如果它不回收,初始化一些
//属性
ImageView的=新ImageView的(mContext);
imageView.setLayoutParams(新GridView.LayoutParams(220,220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8,8,8,8);
} 其他 {
ImageView的=(ImageView的)convertView;
}
位图BM =去codeSampledBitmapFromUri(itemList.get(位置),220,
220);
imageView.setImageBitmap(BM);
返回ImageView的;
}
公共位图德codeSampledBitmapFromUri(字符串路径,诠释reqWidth,
INT reqHeight){
位图BM = NULL;
//第一代code与inJustDe codeBounds = true来检查尺寸
最后BitmapFactory.Options选项=新BitmapFactory.Options();
options.inJustDe codeBounds = TRUE;
BitmapFactory.de codeFILE(路径选择);
//计算inSampleSize
options.inSampleSize = calculateInSampleSize(选项,reqWidth,
reqHeight);
与inSampleSize集//德code位图
options.inJustDe codeBounds = FALSE;
BM = BitmapFactory.de codeFILE(路径选择);
返回BM;
}
公众诠释calculateInSampleSize(
BitmapFactory.Options选项,INT reqWidth,诠释reqHeight){
//原始高度和宽度的图像
最终诠释身高= options.outHeight;
最终诠释宽度= options.outWidth;
INT inSampleSize = 1;
如果(高度> reqHeight ||宽度GT; reqWidth){
如果(宽>高度){
inSampleSize = Math.round((浮点)高度
/(浮点)reqHeight);
} 其他 {
inSampleSize = Math.round((浮点)宽/(浮点)reqWidth);
}
}
返回inSampleSize;
}
}
ImageAdapter myImageAdapter;
@覆盖
公共无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
GridView控件的GridView =(GridView控件)findViewById(R.id.gridview);
myImageAdapter =新ImageAdapter(本);
gridview.setAdapter(myImageAdapter);
字符串ExternalStorageDirectoryPath =环境
.getExternalStorageDirectory()getAbsolutePath()。
字符串TARGETPATH = ExternalStorageDirectoryPath +/测试/;
Toast.makeText(getApplicationContext(),TARGETPATH,Toast.LENGTH_LONG)
。显示();
文件targetDirector =新的文件(TARGETPATH);
文件[]文件= targetDirector.listFiles();
对于(文件文件:文件){
myImageAdapter.add(file.getAbsolutePath());
}
}
}
解决方案
通过图像的其他活动,以显示它在众目睽睽之下。
从您的主要活动传递的图像。使用下面的code:
gridview.setOnItemClickListener(新OnItemClickListener(){
公共无效onItemClick(适配器视图<>母公司,视图V,
INT位置,长的id){
ImageView的IMG = ia.getView(位置,V,父母);
img.buildDrawingCache();
位图BMAP = img.getDrawingCache();
意向意图=新的意图(HelloGridView.this,
Imageviewer.class);
束束=新包();
//bundle.putParcelable("image,BMAP);
字符串面值= myimageadpter.getpath(位置);
bundle.putParcelable(的ImagePath,标准杆);
intent.putExtras(包);
startActivityForResult(意向,0);
}
});
在其他活动中使用这种code,显示传递的图像:
捆绑包= this.getIntent()getExtras()。
BMP = bundle.getParcelable(图像);
ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
D =新BitmapDrawable(BMP);
img.setImageBitmap(BMP);
如果您通过路径。第二项活动是这样的:
捆绑包= this.getIntent()getExtras()。
字符串s = bundle.getParcelable(的ImagePath);
位图Imagefrompath = BitmapFactory.de codeFILE(S);
ImageView的IMG =(ImageView的)findViewById(R.id.imageView1);
img.setImageBitmap(Imagefrompath);
I'm new here and new in programming Android. I found this example (below is the example) on a site in internet, It's a great tutorial! What do I want to achieve is when once I clicked a picture on the GridView I want to show the full size of the Image.
public class MainActivity extends Activity {
public class ImageAdapter extends BaseAdapter {
private Context mContext;
ArrayList<String> itemList = new ArrayList<String>();
public ImageAdapter(Context c) {
mContext = c;
}
void add(String path) {
itemList.add(path);
}
@Override
public int getCount() {
return itemList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some
// attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(220, 220));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
Bitmap bm = decodeSampledBitmapFromUri(itemList.get(position), 220,
220);
imageView.setImageBitmap(bm);
return imageView;
}
public Bitmap decodeSampledBitmapFromUri(String path, int reqWidth,
int reqHeight) {
Bitmap bm = null;
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(path, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
bm = BitmapFactory.decodeFile(path, options);
return bm;
}
public int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
if (width > height) {
inSampleSize = Math.round((float) height
/ (float) reqHeight);
} else {
inSampleSize = Math.round((float) width / (float) reqWidth);
}
}
return inSampleSize;
}
}
ImageAdapter myImageAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
myImageAdapter = new ImageAdapter(this);
gridview.setAdapter(myImageAdapter);
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory().getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/test/";
Toast.makeText(getApplicationContext(), targetPath, Toast.LENGTH_LONG)
.show();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files) {
myImageAdapter.add(file.getAbsolutePath());
}
}
}
解决方案
Pass the image to the other activity to show it in full view.
To pass the image from Your main Activity. USe following code:
gridview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
ImageView img = ia.getView(position, v, parent);
img.buildDrawingCache();
Bitmap bmap = img.getDrawingCache();
Intent intent = new Intent(HelloGridView.this,
Imageviewer.class);
Bundle bundle = new Bundle();
//bundle.putParcelable("image", bmap);
String par=myimageadpter.getpath(position);
bundle.putParcelable("imagepath", par);
intent.putExtras(bundle);
startActivityForResult(intent, 0);
}
});
In other activity use this code to show the passed image:
Bundle bundle = this.getIntent().getExtras();
bmp=bundle.getParcelable("image");
ImageView img=(ImageView) findViewById(R.id.imageView1);
d =new BitmapDrawable(bmp);
img.setImageBitmap(bmp);
If you pass path. the second activity look like this:
Bundle bundle = this.getIntent().getExtras();
String s=bundle.getParcelable("imagepath");
Bitmap Imagefrompath = BitmapFactory.decodeFile(s);
ImageView img=(ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(Imagefrompath );
这篇关于我如何能显示图片的全尺寸?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文