読者です 読者をやめる 読者になる 読者になる

liguofeng29’s blog

個人勉強用ブログだっす。

AndroidのUI - ImageSwitcher

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

<GridView
android:id="@+id/gridView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="2dp"
android:verticalSpacing="2dp"
android:gravity="center"
android:numColumns="4" />

<ImageSwitcher
android:id="@+id/imageSwitcher"
android:layout_width="400dp"
android:layout_height="400dp"

android:inAnimation="@android:anim/fade_in"
android:outAnimation="@android:anim/fade_out"

></ImageSwitcher>

</LinearLayout>
 
cell.xml
 
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:padding="4dp">
<ImageView
android:id="@+id/image1"
android:layout_width="50dp"
android:layout_height="50dp"
/>
</LinearLayout>
 

 

MainActivity.java
 
package com.example.liguofeng.imageswitcher;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.SimpleAdapter;
import android.widget.ViewSwitcher;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {
int[] images = {
R.drawable.bomb5, R.drawable.bomb6, R.drawable.bomb7, R.drawable.bomb8,
R.drawable.bomb9, R.drawable.bomb10, R.drawable.bomb11, R.drawable.bomb12,
R.drawable.bomb13, R.drawable.bomb14, R.drawable.bomb15, R.drawable.bomb16,
};

ImageSwitcher imageSwitcher;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// ImageSwitcher設定
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
// ImageView生成
ImageView imageView = new ImageView(MainActivity.this);

imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

imageView.setLayoutParams(new ImageSwitcher.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT
));

return imageView;
}
});

List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();

for (int i = 0; i < images.length; i++) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("image", images[i]);
itemList.add(map);
}

SimpleAdapter adapter = new SimpleAdapter(this,
itemList,
R.layout.cell,
new String[]{"image"},
new int[]{R.id.image1});

GridView gridView = (GridView) findViewById(R.id.gridView);
// apdater設定
gridView.setAdapter(adapter);
// イベント設定
gridView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
imageSwitcher.setImageResource(images[position]);
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
imageSwitcher.setImageResource(images[position]);
}
});
}
}