如何在数组中随机播放图像?



我正在使用Java代码,但我不知道如何打乱图像和文本。这是我所有的编码。它们在不同的类中,所以我将单独发布它们。这是在Android Studio中。

这是我的ImageandTextAdapter代码:

public class ImageAndTextAdapter extends ArrayAdapter<String> {
private LayoutInflater mInflater;
private  String[] mStrings;
private TypedArray mIcons;
private int mViewResourceId;
public ImageAndTextAdapter (Context ctx, int ViewResourceId, String[] strings, TypedArray icons) {
super(ctx, ViewResourceId, strings);
mInflater = (LayoutInflater)ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mStrings = strings;
mIcons = icons;
mViewResourceId = ViewResourceId;
}
@Override
public int getCount () { return mStrings.length; }
@Override
public String getItem(int position) {return mStrings[position]; }
@Override
public long getItemId(int position) {return 0; }
@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
iv.setImageDrawable(mIcons.getDrawable(position));
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(mStrings[position]);
return convertView;
}
}

这是我的图片数组代码:

<resources>
<array name="card_faces">
<item>@drawable/b01</item>
<item>@drawable/b02</item>
<item>@drawable/b03</item>
<item>@drawable/b04</item>
<item>@drawable/b05</item>
<item>@drawable/b06</item>
<item>@drawable/b07</item>
<item>@drawable/b08</item>
</array>
</resources>

这是我的主要活动代码:

public class MainActivity extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Context context = getApplicationContext();
Resources resources = context.getResources();
String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};
TypedArray card_faces = resources.obtainTypedArray(R.array.card_faces);
setListAdapter(new ImageAndTextAdapter (context, R.layout.secondary_layout, card, card_faces));
}
}

我想打乱数组中设置的图片,但我无法使用 Collections.shuffle 命令。如果您知道我可以使用不同且简单的方法打乱图片的方法,请提供帮助。

您可以通过将数组转换为列表和随机播放,然后转换回数组来做到这一点

String[] card = {"Blue1", "Blue2", "BlueSnake", "Blue4", "Blue5", "Blue6", "Blue7", "BlueHawk"};
List<String> cardList = Arrays.asList(card);
Collections.shuffle(cardList);
cardList.toArray(card);

如果您不想使用更多空间,可以尝试使用随机数生成器手动随机播放。

private static <T> void shuffle(T[] input){
Random rand = new Random();
for (int i = 0; i < input.length; i++) {
swap(input, i, rand.nextInt(input.length));
}
}
private static <T> void swap(T[] input, int x, int y){
T tmp = input[x];
input[x] = input[y];
input[y] = tmp;
}

编辑

如果你想洗牌TypedArray中的图像,而不是洗牌字符串,你可以简单地创建一个带有索引的int数组并洗牌它们,然后对字符串和图像使用它的位置。

不要打乱字符串

List<Integer> indexes = new ArrayList<>();
for(int i = 0; i < indexes.length; i++){
indexes.add(i);
}
Collections.shuffle(indexes);

使用

@Override
public View getView(int position, View convertView, ViewGroup parent) {
convertView = mInflater.inflate(mViewResourceId, null);
int newPosition = indexes.get(position);
ImageView iv = (ImageView) convertView.findViewById(R.id.imageView);
iv.setImageDrawable(mIcons.getDrawable(newPosition));
TextView tv = (TextView)convertView.findViewById(R.id.textView);
tv.setText(mStrings[newPosition]);
return convertView;
}

最新更新