在android中的imageview上向左或向右滑动时启动动画

时间:2017-06-12 08:54:16

标签: android android-animation

我有一个在按下时动画的图像视图,我要做的是在滑动视图时启动动画!因此,如果向右滑动则动画向右移动,如果向左滑动则动画反转!下面的代码就是我现在的位置。

public class MainActivity extends AppCompatActivity {
Context context = this;
MediaPlayer mp;

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

    ArrayList<ItemData> list = new ArrayList<>();
    list.add(new ItemData(getString(R.string.red), R.drawable.red));
    list.add(new ItemData(getString(R.string.blue), R.drawable.blue));
    list.add(new ItemData(getString(R.string.green), R.drawable.green));
    list.add(new ItemData(getString(R.string.orange), R.drawable.orange));
    list.add(new ItemData(getString(R.string.pink), R.drawable.pink));
    list.add(new ItemData(getString(R.string.yellow), R.drawable.yel));


    final Spinner sp = (Spinner) findViewById(R.id.spinner);
    SpinnerAdapter adapter = new SpinnerAdapter(this,
            R.layout.spinner_layout, R.id.txt, list);
    sp.setAdapter(adapter);

    mp = MediaPlayer.create(context, R.raw.sound);

    //my code from start
    final ImageView myView = (ImageView) findViewById(R.id.mainimage);

    sp.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
            ItemData data = (ItemData) sp.getSelectedItem();
            int res = data.getImageId();  //change Integer type to int  if error exists
            myView.setImageDrawable(getResources().getDrawable(res));
        }

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

        }
    });

    myView.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            try {
                if (mp.isPlaying()) {
                    mp.stop();
                    mp.release();
                    mp = MediaPlayer.create(context, R.raw.fidget);
                }
                mp.start();
            } catch (Exception e) {
                e.printStackTrace();
            }

            ImageView myView = (ImageView) findViewById(R.id.mainimage);
            Animation a = new RotateAnimation(0.0f, 360.0f,
                    Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
                    0.5f);
            a.setInterpolator(new LinearInterpolator());
            a.setRepeatCount(-1);
            a.setDuration(500);
            a.setFillAfter(true);
            a.setRepeatCount(7);
            myView.startAnimation(a);

        }
    });
}
}

项目数据类是

public class ItemData {

String text;
Integer imageId;
public ItemData(String text, Integer imageId){
    this.text=text;
    this.imageId=imageId;
}

public String getText(){
    return text;
}

public Integer getImageId(){
    return imageId;
}

}

微调器适配器

public class SpinnerAdapter extends ArrayAdapter<ItemData> {

int groupid;
Activity context;
ArrayList<ItemData> list;
LayoutInflater inflater;
public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemData>
        list){
    super(context,id,list);
    this.list=list;
    inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    this.groupid=groupid;
}

public View getView(int position, View convertView, ViewGroup parent ){
    View itemView=inflater.inflate(groupid,parent,false);
    ImageView imageView=(ImageView)itemView.findViewById(R.id.img);
    imageView.setImageResource(list.get(position).getImageId());
    TextView textView=(TextView)itemView.findViewById(R.id.txt);
    textView.setText(list.get(position).getText());
    return itemView;
}

public View getDropDownView(int position, View convertView, ViewGroup
        parent){
    return getView(position,convertView,parent);

}
}

0 个答案:

没有答案