package com.xpc.picture;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.FrameLayout.LayoutParams;
import android.widget.GridView;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
/*
*一直都是空指針異常,求大神指點一點
*/
public class MainActivity extends Activity {
private int[] imageId = new int[]{R.drawable.a,R.drawable.b,R.drawable.c,
R.drawable.e,R.drawable.f,R.drawable.g,R.drawable.h,
R.drawable.i};//定義並初始化保存圖片IDde 數組
private ImageSwitcher imageSwitcher; //申明一個圖像切換器對象
GridView gridview = (GridView) findViewById(R.id.gridview1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageSwitcher = (ImageSwitcher) findViewById(R.id.imageSwitcher1);
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));//設置淡入動畫
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));//淡出效果
imageSwitcher.setFactory(new ViewFactory() {
@Override
public View makeView() {
// TODO Auto-generated method stub
ImageView imageView = new ImageView(MainActivity.this);//實例化一個ImageView類的對象
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));//設置保持縱橫比居中縮放圖像
//是否導包錯誤???這是要導什麼包,太多了
return imageView;
}
});
imageSwitcher.setImageResource(imageId[6]);
BaseAdapter adapter = new BaseAdapter(){
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageId.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView imageview;
if(convertView==null){
imageview = new ImageView(MainActivity.this);
imageview.setAdjustViewBounds(true);
imageview.setMaxWidth(150);//設置長度寬度
imageview.setMaxHeight(113);
imageview.setPadding(5,5, 5,5);//設置內邊距
}else{
imageview=(ImageView) convertView;
}
imageview.setImageResource(imageId[position]);//設置要顯示的圖片
return imageview;
}
};
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
imageSwitcher.setImageResource(imageId[position]);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局文件
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<GridView
android:id="@+id/gridview1"
android:layout_height="match_parent"
android:layout_width="640dp"
android:layout_marginTop="10dp"
android:horizontalSpacing="3dp"
android:verticalSpacing="3dp"
android:numColumns="4"
/>
<ImageSwitcher
android:id="@+id/imageSwitcher1"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
GridView gridview = (GridView) findViewById(R.id.gridview1);應該寫在setContentView(R.layout.activity_main);後面的