我制作电视应用程序演示。我想让选定的项目更大。
来自movies.xml
的数据
我的代码。
public class MainActivity extends Activity {
private XmlPullParser xpp;
private InputStream input;
private List<Movie> movieList = new ArrayList<Movie>();
private MyAdapter adapter;
private GridView gridview;
private static final String MOVIE = "movie";
private static final String ID = "id";
private static final String TITLE = "title";
private static final String BACKGROUND = "background";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gridview = (GridView) findViewById(R.id.gridview);
movieList = createMovies();
adapter = new MyAdapter(this, movieList);
gridview.setAdapter(adapter);
gridview.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
String str = "123456";
}
/*
* 从xml中获取movies的信息
* */
public List<Movie> createMovies() {
List<Movie> resultList = new ArrayList<Movie>();
xpp = Xml.newPullParser();
Movie item = null;
try {
input = getResources().getAssets().open("movies.xml");
xpp.setInput(input,"utf-8");
int type = xpp.getEventType();
while(type!=XmlPullParser.END_DOCUMENT){
switch(type){
case XmlPullParser.START_TAG:
String name = xpp.getName();
if(name.equalsIgnoreCase(MOVIE)){
item = new Movie();
}else if(item!=null){
if(name.equalsIgnoreCase(ID)){
item.setId(Integer.parseInt(xpp.nextText()));
}else if(name.equalsIgnoreCase(TITLE)){
item.setTitle(xpp.nextText());
}
else if(name.equalsIgnoreCase(BACKGROUND)){
item.setBackground(xpp.nextText());
}
}
break;
case XmlPullParser.END_TAG:
if(xpp.getName().equalsIgnoreCase(MOVIE) && item!=null){
resultList.add(item);
item = null;
}
default:
break;
}
type = xpp.next();
}
} catch (Exception e) {
e.printStackTrace();
}
return resultList;
}
}
我的自定义适配器。我做了一个测试,使第二个项目更大ObjectAnimator
。但它不起作用。
public class MyAdapter extends BaseAdapter {
private Context mContext;
private LayoutInflater mInflater;
private List<Movie> movieList;
private ObjectAnimator mZoomInAnimation;
private ObjectAnimator mZoomOutAnimation;
public MyAdapter(Context context,List<Movie> list){
mContext = context;
mInflater = LayoutInflater.from(mContext);
movieList = list;
initAnimation();
}
@Override
public int getCount() {
return movieList.size();
}
@Override
public Object getItem(int position) {
return movieList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(null == convertView){
convertView = mInflater.inflate(R.layout.item_grid_view, null);
holder = new ViewHolder();
holder.view = convertView.findViewById(R.id.border);
LayoutParams params = (LayoutParams) holder.view.getLayoutParams();
params.height = 300 + 15;
params.width = 250 + 15;
holder.background = (ImageView) convertView.findViewById(R.id.background);
LayoutParams backLayoutParams = (LayoutParams) holder.background.getLayoutParams();
backLayoutParams.height = 300;
backLayoutParams.width = 250;
holder.title = (TextView) convertView.findViewById(R.id.title);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.title.setText(movieList.get(position).getTitle());
// int i = mContext.getResources().getIdentifier("drawable/photography1", null, mContext.getPackageName());
// holder.background.setImageResource(i);
AssetManager assetManager = mContext.getAssets();
InputStream istr;
String url = movieList.get(position).getBackground();
String str = url.substring(9);
try {
istr = assetManager.open(str);
Bitmap bitmap = BitmapFactory.decodeStream(istr);
holder.background.setBackgroundDrawable(new BitmapDrawable(bitmap));
istr.close();
} catch (IOException e) {
e.printStackTrace();
}
//mContext.getResources().getDrawable(R.drawable.list_detail_item_border).getConstantState()
// Boolean is = holder.background.getDrawable().getCurrent().getConstantState().equals(null)?true:false;
if(position == 1){ //如果view的图片是list_detail_item_border则一个动画
if(mZoomInAnimation!=null){
mZoomOutAnimation.cancel();
mZoomInAnimation.start();
}
}
else{ // 如果不是则另外一个动画
if(mZoomOutAnimation!=null){
mZoomInAnimation.cancel();
mZoomOutAnimation.start();
}
}
return convertView;
}
public class ViewHolder{
public View view;
public ImageView background;
public TextView title;
}
public void initAnimation() {
float endVal = 2.2f;
PropertyValuesHolder inScaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f, endVal);
PropertyValuesHolder inScaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f, endVal);
mZoomInAnimation = new ObjectAnimator();
mZoomInAnimation.setTarget(this);
mZoomInAnimation.setValues(inScaleX, inScaleY);
mZoomInAnimation.setDuration(200);
PropertyValuesHolder outScaleX = PropertyValuesHolder.ofFloat("scaleX", endVal, 1.0f);
PropertyValuesHolder outScaleY = PropertyValuesHolder.ofFloat("scaleY", endVal, 1.0f);
mZoomOutAnimation = new ObjectAnimator();
mZoomOutAnimation.setTarget(this);
mZoomOutAnimation.setValues(outScaleX, outScaleY);
mZoomOutAnimation.setDuration(200);
}