Advertisement
keneduck

Untitled

Jul 15th, 2013
366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.07 KB | None | 0 0
  1. package com.haarman.listviewanimations;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Collection;
  5. import java.util.List;
  6. import java.util.Timer;
  7. import java.util.TimerTask;
  8.  
  9. import android.app.Activity;
  10. import android.content.Context;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. import android.os.Bundle;
  14. import android.support.v4.util.LruCache;
  15. import android.view.LayoutInflater;
  16. import android.view.View;
  17. import android.view.ViewGroup;
  18. import android.widget.AbsListView;
  19. import android.widget.AdapterView;
  20. import android.widget.AdapterView.OnItemClickListener;
  21. import android.widget.ImageView;
  22. import android.widget.ListView;
  23. import android.widget.TextView;
  24. import android.widget.Toast;
  25.  
  26. import com.haarman.listviewanimations.itemmanipulation.SwipeDismissAdapter;
  27. import com.haarman.listviewanimations.itemmanipulation.OnDismissCallback;
  28. import com.haarman.listviewanimations.swinginadapters.prepared.SwingBottomInAnimationAdapter;
  29. import com.ducky.animations.FlipAnimation;
  30. import com.example.mycard.R;
  31.  
  32. public class GoogleCardsActivity1 extends Activity implements OnDismissCallback {
  33.  
  34. private GoogleCardsAdapter mGoogleCardsAdapter;
  35. private static ListView listView;
  36.  
  37. @Override
  38. protected void onCreate(Bundle savedInstanceState) {
  39. super.onCreate(savedInstanceState);
  40. setContentView(R.layout.activity_googlecards);
  41.  
  42. listView = (ListView) findViewById(R.id.activity_googlecards_listview);
  43.  
  44. mGoogleCardsAdapter = new GoogleCardsAdapter(this);
  45. SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(
  46. new SwipeDismissAdapter(mGoogleCardsAdapter, this));
  47. swingBottomInAnimationAdapter.setAbsListView(listView);
  48.  
  49. listView.setAdapter(swingBottomInAnimationAdapter);
  50.  
  51. mGoogleCardsAdapter.addAll(getItems());
  52.  
  53. }
  54.  
  55. private ArrayList<Integer> getItems() {
  56. ArrayList<Integer> items = new ArrayList<Integer>();
  57.  
  58. for (int i = 0; i < 100; i++) {
  59. items.add(i);
  60. }
  61.  
  62. return items;
  63.  
  64. }
  65.  
  66. @Override
  67. public void onDismiss(AbsListView listView, int[] reverseSortedPositions) {
  68. for (int position : reverseSortedPositions) {
  69. mGoogleCardsAdapter.remove(position);
  70.  
  71. }
  72. }
  73.  
  74. private class GoogleCardsAdapter extends ArrayAdapter<Integer> {
  75.  
  76. private Context mContext;
  77. private LruCache<Integer, Bitmap> mMemoryCache;
  78. ListView listView = (ListView) findViewById(R.id.activity_googlecards_listview);
  79.  
  80. public GoogleCardsAdapter(Context context) {
  81. mContext = context;
  82.  
  83. final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024);
  84.  
  85. // Use 1/8th of the available memory for this memory cache.
  86. final int cacheSize = maxMemory;
  87. mMemoryCache = new LruCache<Integer, Bitmap>(cacheSize) {
  88. @Override
  89. protected int sizeOf(Integer key, Bitmap bitmap) {
  90. // The cache size will be measured in kilobytes rather than
  91. // number of items.
  92. return bitmap.getRowBytes() * bitmap.getHeight() / 1024;
  93. }
  94. };
  95. TimerTask mytask = new TimerTask() {
  96. @Override
  97. public void run() {
  98. // TODO Auto-generated method stub
  99. resetscroll();
  100. }
  101. };
  102. Timer timer = new Timer();
  103. timer.scheduleAtFixedRate(mytask, 150, 150);
  104.  
  105. // ----------------------------------------
  106. // This flip card when item is selected
  107. // ----------------------------------------
  108. listView.setOnItemClickListener(new OnItemClickListener() {
  109. public void onItemClick(AdapterView<?> parent, View view,
  110. int position, long id) {
  111.  
  112. flipCard(position);
  113. String currentposition;
  114.  
  115. StringBuilder sb1 = new StringBuilder();
  116. sb1.append(position);
  117. currentposition = sb1.toString();
  118.  
  119. Toast.makeText(getApplicationContext(), currentposition,
  120. Toast.LENGTH_LONG).show();
  121.  
  122. }
  123.  
  124. });
  125.  
  126. }
  127.  
  128. @Override
  129. public View getView(int position, View convertView, ViewGroup parent) {
  130. ViewHolder viewHolder;
  131. View view = convertView;
  132. if (view == null) {
  133. view = LayoutInflater.from(mContext).inflate(
  134. R.layout.activity_googlecards_card, parent, false);
  135.  
  136. viewHolder = new ViewHolder();
  137. viewHolder.textView = (TextView) view
  138. .findViewById(R.id.activity_googlecards_card_textview);
  139. view.setTag(viewHolder);
  140.  
  141. viewHolder.imageView = (ImageView) view
  142. .findViewById(R.id.activity_googlecards_card_imageview);
  143. } else {
  144. viewHolder = (ViewHolder) view.getTag();
  145.  
  146. }
  147.  
  148. viewHolder.textView.setText("This is card "
  149. + (getItem(position) + 1));
  150.  
  151. setImageView(viewHolder, position);
  152.  
  153. return view;
  154. }
  155.  
  156. private void flipCard(int position) {
  157.  
  158. View rootLayout = listView.getChildAt(position);
  159. View cardFace = listView.getChildAt(position);
  160. View cardBack = listView.getChildAt(1);
  161.  
  162. FlipAnimation flipAnimation = new FlipAnimation(cardFace, cardBack);
  163.  
  164. if (cardFace.getVisibility() == View.GONE) {
  165. flipAnimation.reverse();
  166. }
  167. rootLayout.startAnimation(flipAnimation);
  168.  
  169. }
  170.  
  171. public void resetscroll() {
  172. listView.smoothScrollToPosition(0, 0);
  173.  
  174. }
  175.  
  176. private void setImageView(ViewHolder viewHolder, int position) {
  177. int imageResId;
  178. switch (getItem(position) % 5) {
  179. case 0:
  180. imageResId = R.drawable.img_nature1;
  181. break;
  182. case 1:
  183. imageResId = R.drawable.img_nature2;
  184. break;
  185. case 2:
  186. imageResId = R.drawable.img_nature3;
  187. break;
  188. case 3:
  189. imageResId = R.drawable.img_nature4;
  190. break;
  191. default:
  192. imageResId = R.drawable.img_nature5;
  193. }
  194.  
  195. Bitmap bitmap = getBitmapFromMemCache(imageResId);
  196. if (bitmap == null) {
  197. bitmap = BitmapFactory.decodeResource(mContext.getResources(),
  198. imageResId);
  199. addBitmapToMemoryCache(imageResId, bitmap);
  200. }
  201. viewHolder.imageView.setImageBitmap(bitmap);
  202. }
  203.  
  204. private void addBitmapToMemoryCache(int key, Bitmap bitmap) {
  205. if (getBitmapFromMemCache(key) == null) {
  206. mMemoryCache.put(key, bitmap);
  207. }
  208. }
  209.  
  210. private Bitmap getBitmapFromMemCache(int key) {
  211. return mMemoryCache.get(key);
  212. }
  213.  
  214. private class ViewHolder {
  215. TextView textView;
  216. ImageView imageView;
  217. }
  218.  
  219. }
  220. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement