gpt4 book ai didi

android - 更改在android中单击的按钮的图库 View

转载 作者:行者123 更新时间:2023-11-30 04:28:43 24 4
gpt4 key购买 nike

我有 Image 、 Videos 、 Audios 三个按钮。我想在单击相应按钮时使用音频、视频、图像资源更改图库中的 View ,以便只有按钮的相关图像出现在图库中。

我的代码在这里:

XML 布局:

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Audios" >
</Button>

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Videos" >
</Button>

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Images" >
</Button>
</LinearLayout>

<Gallery
android:id="@+id/Gallery01"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</Gallery>
</LinearLayout>

<ImageView
android:id="@+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ImageView>

Java文件:

public class GalleryView extends Activity {
Integer[] pics = {
R.drawable.cloudy,
R.drawable.hazy,
R.drawable.mostlycloudyday,
R.drawable.partlycloud,
R.drawable.sunny,
R.drawable.sunrain,
R.drawable.thunderstorm,
R.drawable.weathercloudy,
};
ImageView imageView;
Button mAudio;
Button mVideo;
Button mImages;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio=(Button) findViewById(R.id.button1);
Gallery ga = (Gallery)findViewById(R.id.Gallery01);
ga.setAdapter(new ImageAdapter(this));

imageView = (ImageView)findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
long arg3) {
Toast.makeText(getBaseContext(),
"You have selected picture " + (pos+1) + "of Gallery",
Toast.LENGTH_SHORT).show();
imageView.setImageResource(pics[pos]);

}

});

}


public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;

public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}

@Override
public int getCount() {

return pics.length;
}

@Override
public Object getItem(int arg0) {

return arg0;
}

@Override
public long getItemId(int arg0) {

return arg0;
}

@Override
public View getView(int arg0, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(pics[arg0]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150,120));
iv.setBackgroundResource(imageBackground);
return iv;
}

}
}

谁能帮帮我,

谢谢。

最佳答案

要更改图库中的图像,您必须更改数组中的资源图像 ID。例如,更改 int[] video_pics 中视频的图像等等。

import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;

public class GalleryView extends Activity implements OnClickListener {
int[] image_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

int[] video_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

int[] audio_pics = { R.drawable.cloudy, R.drawable.hazy, R.drawable.mostlycloudyday, R.drawable.partlycloud,
R.drawable.sunny, R.drawable.sunrain, R.drawable.thunderstorm, R.drawable.weathercloudy, };

ImageView imageView;

Button mAudio;
Button mVideo;
Button mImages;

ImageAdapter galleryAdapter;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mAudio = (Button) findViewById(R.id.button1);
mVideo = (Button) findViewById(R.id.button2);
mImages = (Button) findViewById(R.id.button3);

mAudio.setOnClickListener(this);
mVideo.setOnClickListener(this);
mImages.setOnClickListener(this);

Gallery ga = (Gallery) findViewById(R.id.Gallery01);
galleryAdapter = new ImageAdapter(this);
galleryAdapter.setResourseArray(image_pics);
ga.setAdapter(galleryAdapter);

imageView = (ImageView) findViewById(R.id.ImageView01);
ga.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
Toast.makeText(getBaseContext(), "You have selected picture " + (pos + 1) + "of Gallery", Toast.LENGTH_SHORT)
.show();
imageView.setImageResource(pics[pos]);

}

});
}

@Override
public void onClick(View view) {
if (view == mAudio) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(audio_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mVideo) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(video_pics);
galleryAdapter.notifyDataSetChanged();
}
} else if (view == mImages) {
if (galleryAdapter != null) {
galleryAdapter.setResourseArray(image_pics);
galleryAdapter.notifyDataSetChanged();
}
}
}

public class ImageAdapter extends BaseAdapter {

private Context ctx;
int imageBackground;
private int[] resourseArray = null;

public ImageAdapter(Context c) {
ctx = c;
TypedArray ta = obtainStyledAttributes(R.styleable.Gallery1);
imageBackground = ta.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 1);
ta.recycle();
}

@Override
public int getCount() {
return resourseArray.length;
}

public int[] getResourseArray() {
return resourseArray;
}

public void setResourseArray(int[] resourseArray) {
this.resourseArray = resourseArray;
}

@Override
public Object getItem(int arg0) {

return arg0;
}

@Override
public long getItemId(int arg0) {

return arg0;
}

@Override
public View getView(int position, View arg1, ViewGroup arg2) {
ImageView iv = new ImageView(ctx);
iv.setImageResource(resourseArray[position]);
iv.setScaleType(ImageView.ScaleType.FIT_XY);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
iv.setBackgroundResource(imageBackground);
return iv;
}

}
}

关于android - 更改在android中单击的按钮的图库 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8075788/

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