动画中的ImageView动画

时间:2012-12-06 13:54:32

标签: android android-fragments android-animation android-imageview

无法通过onItemClickListener ListView的方法为ImageView设置动画。 ListView位于Fragment中。有人可以建议我该怎么办?

public class MainFragment extends Fragment {

View view;



public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
    View view = inflater.inflate(R.layout.fragment_layout, container, false);
    ListView lv = (ListView)view.findViewById(R.id.listf);
    this.view= view;


lv.setOnItemClickListener(new OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){


          ImageView i = (ImageView)view.findViewById(R.id.imgv);
          AnimationSet as = new AnimationSet(true);
          Animation animation1 = AnimationUtils.loadAnimation(this, R.anim.anim1);              
          Animation animation2 = AnimationUtils.loadAnimation(this, R.anim.anim2);
          as.addAnimation(animation1);
          as.addAnimation(animation2);
          i.setImageResource(R.drawable.logocpic1);
          i.startAnimation(as);
    }
}

3 个答案:

答案 0 :(得分:2)

I think you have to try this following code it works for me....

动画animation1 = AnimationUtils.loadAnimation(getapplicationContext(),R.anim.anim1);

而不是

动画animation1 = AnimationUtils.loadAnimation(this,R.anim.anim1);

可能适用于你。

答案 1 :(得分:1)

好的,您遇到了错误,因为您在非UI线程中进行UI操作。你可以使用下面的代码,

public class MainFragment extends Fragment {
View view;
ImageView i;
.
.
.
.

lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
.
.
.   


   getActivity().runOnUiThread(new Runnable() {
      public void run() {
         i = (ImageView)getActivity().findViewById(R.id.imgv);
      }
   });

答案 2 :(得分:0)

请改为喜欢

Animation animation1 = AnimationUtils.loadAnimation(MainFragment.this, R.anim.anim1);