gpt4 book ai didi

android - 如何显示线性布局中一组图像中的点击图像?

转载 作者:行者123 更新时间:2023-11-29 02:06:02 26 4
gpt4 key购买 nike

我从 SD 卡文件夹中获取图像并将它们动态添加到位于 Horizo​​ntalScrollview 内的 LinearLayout。现在,当我单击图像时,我想在与大图像相同的 Activity 中的另一个 ImageView 中显示该特定图像。我怎样才能做到这一点?我尝试获取 getId(),getTag() 但我做不到,请给我建议。注意:由于中心锁定功能,我不想使用图库小部件。我的代码:

    package com.pop.cam;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class GalleryView extends Activity {

ImageView iv;
File[] sdDirFiles;
LinearLayout linearLayout;
int i;
String path;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pictures);
linearLayout = (LinearLayout) findViewById(R.id.linearView);
iv = (ImageView) findViewById(R.id.ImageView);

// getting images from SD card folder
File sdDir = new File("/sdcard/Pictures/"
+ MyCameraAppActivity.DIR_NAME);
sdDirFiles = sdDir.listFiles();
for (i = 0; i < sdDirFiles.length; i++) {
final ImageView imageView = new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setLayoutParams(new LayoutParams(130, 100));
Bitmap b = decodeFile(sdDirFiles[i].getAbsoluteFile());
imageView.setImageBitmap(b);
path = sdDirFiles[i].getAbsolutePath();
imageView.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// Here i want to set clicked image as big.
//I tried following but not workin
iv.setImageResource(v.getId());
iv.setImageURI(Uri.parse(path));
Bitmap b = decodeFile(sdDirFiles[Integer.valueOf(v.getId())].getAbsoluteFile());
iv.setImageBitmap(b);

}
});
linearLayout.addView(imageView);
}
}

// decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 70;

// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
}
return null;
}
}

图片.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<HorizontalScrollView
android:id="@+id/horizontalScorllView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >

<LinearLayout
android:id="@+id/linearView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</HorizontalScrollView>

<ImageView
android:id="@+id/ImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:contentDescription="@string/app_name" >
</ImageView>

</LinearLayout>

最佳答案

imageView.setOnClickListener(new View.OnClickListener() {

之前添加下面一行
imageView.setId(i);

使用下面的行代替 iv.setImageResource(v.getId());

int id = v.getId();
Bitmap b = decodeFile(sdDirFiles[id].getAbsoluteFile()); // this gives small image bcoz you implemented decodeFile like that.
iv.setImageBitmap(b);

int id = v.getId();                 
Uri uri = Uri.fromFile(sdDirFiles[id].getAbsoluteFile()); // it will produce original resolution or fit to screen
iv.setImageURI(uri);

我测试了这段代码,它工作正常。

关于android - 如何显示线性布局中一组图像中的点击图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9988615/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com