1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
| public class MainActivity extends Activity {
private ListView mListView; private ImageView shopCart; private ImageView buyImg; private ViewGroup anim_mask_layout;
private Context mContext; private ProductAdapter productAdapter;
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); mContext = this; initView(); }
private void initView() { shopCart = (ImageView) findViewById(R.id.shopping_img_cart); mListView = (ListView) findViewById(R.id.product_list); productAdapter = new ProductAdapter(); mListView.setAdapter(productAdapter); }
private class ProductAdapter extends BaseAdapter {
public ProductAdapter() { }
@Override public int getCount() { return 20; }
@Override public String getItem(int position) { return "商品" + (position + 1); }
@Override public long getItemId(int position) { return position; }
@Override public View getView(int position, View convertView, final ViewGroup parent) { String name = getItem(position); ViewHolder holder = null; if (convertView == null) { convertView = LayoutInflater.from(mContext).inflate( R.layout.item, null); holder = new ViewHolder(); holder.nameTxt = (TextView) convertView.findViewById(R.id.name); holder.buyBtn = (Button) convertView .findViewById(R.id.product_buy_btn); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } holder.nameTxt.setText(name); holder.buyBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { int[] start_location = new int[2]; v.getLocationInWindow(start_location); buyImg = new ImageView(mContext); buyImg.setImageResource(R.drawable.sign); setAnim(buyImg, start_location); } }); return convertView; }
class ViewHolder { TextView nameTxt; Button buyBtn; }
}
private ViewGroup createAnimLayout() { ViewGroup rootView = (ViewGroup) this.getWindow().getDecorView(); LinearLayout animLayout = new LinearLayout(this); LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT); animLayout.setLayoutParams(lp); animLayout.setId(Integer.MAX_VALUE); animLayout.setBackgroundResource(android.R.color.transparent); rootView.addView(animLayout); return animLayout; }
private View addViewToAnimLayout(final ViewGroup vg, final View view, int[] location) { int x = location[0]; int y = location[1]; LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); lp.leftMargin = x; lp.topMargin = y; view.setLayoutParams(lp); return view; }
private void setAnim(final View v, int[] start_location) { anim_mask_layout = null; anim_mask_layout = createAnimLayout(); anim_mask_layout.addView(v); final View view = addViewToAnimLayout(anim_mask_layout, v, start_location); int[] end_location = new int[2]; shopCart.getLocationInWindow(end_location); int endX = 0 - start_location[0] + 40; int endY = end_location[1] - start_location[1]; Log.d("---", endX+""+endY); TranslateAnimation translateAnimationX = new TranslateAnimation(0, endX, 0, 0); translateAnimationX.setInterpolator(new LinearInterpolator()); translateAnimationX.setRepeatCount(0); translateAnimationX.setFillAfter(true);
TranslateAnimation translateAnimationY = new TranslateAnimation(0, 0, 0, endY); translateAnimationY.setInterpolator(new AccelerateInterpolator()); translateAnimationY.setRepeatCount(0); translateAnimationX.setFillAfter(true);
AnimationSet set = new AnimationSet(false); set.setFillAfter(false); set.addAnimation(translateAnimationY); set.addAnimation(translateAnimationX); set.setDuration(Math.abs(endY/1)); v.startAnimation(set); set.setAnimationListener(new AnimationListener() { @Override public void onAnimationStart(Animation animation) { v.setVisibility(View.VISIBLE); }
@Override public void onAnimationRepeat(Animation animation) { }
@Override public void onAnimationEnd(Animation animation) { v.setVisibility(View.GONE); } }); } }
|