Advertisement
Guest User

Untitled

a guest
Jun 19th, 2013
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.54 KB | None | 0 0
  1. So my issue is I have urls that I am parsing/downloading from my server and I am displaying them in GridView and once the user onItemClicks it triggers this and I am have used this piece of code for the Animation (that I didn't write) that will enlarge the image and then the use has the option to onClick the image launch a SplashActivity or press a button that stops the animation. The Animation works great with R.drawable.image but I can't get the animation to work when it is not a drawable....I am noob so please excuse the ugly code and my lack of complete understanding.
  2.  
  3. import android.animation.Animator;
  4. import android.animation.AnimatorListenerAdapter;
  5. import android.animation.AnimatorSet;
  6. import android.animation.ObjectAnimator;
  7.  
  8. import android.app.Activity;
  9.  
  10. import android.content.Context;
  11. import android.content.Intent;
  12.  
  13. import android.graphics.Point;
  14. import android.graphics.Rect;
  15.  
  16. import android.os.Bundle;
  17.  
  18. import android.view.LayoutInflater;
  19. import android.view.View;
  20. import android.view.ViewGroup;
  21. import android.view.animation.DecelerateInterpolator;
  22.  
  23. import android.widget.AdapterView;
  24. import android.widget.AdapterView.OnItemClickListener;
  25. import android.widget.BaseAdapter;
  26. import android.widget.Button;
  27. import android.widget.GridView;
  28. import android.widget.ImageView;
  29. import android.widget.TextView;
  30. import android.widget.Toast;
  31.  
  32. import com.example.MainActivity;
  33. import com.example.R;
  34. import com.example.reader.image.ImageLoader;
  35. import com.example.parser.RSSFeed;
  36. import com.example.parser.RSSItem;
  37.  
  38. //~--- JDK imports ------------------------------------------------------------
  39.  
  40. import java.util.ArrayList;
  41. import java.util.List;
  42.  
  43. public class ListActivity extends Activity {
  44. protected static final String TAG = null;
  45. RSSFeed mfeed;
  46. private GridView gv;
  47. Button button;
  48. String feedContent;
  49. List<RSSItem> itemPos;
  50. int imageResId;
  51. CustomListAdapter adapter;
  52. private Animator mCurrentAnimator;
  53. private int mShortAnimationDuration;
  54.  
  55. @Override
  56. public void onCreate(Bundle savedInstanceState) {
  57. super.onCreate(savedInstanceState);
  58. setContentView(R.layout.main_activity_anim_list);
  59. button = (Button) findViewById(R.id.button);
  60. mfeed = (RSSFeed) getIntent().getExtras().get("feed");
  61.  
  62. // Initialize the variables:
  63. gv = (GridView) findViewById(R.id.gridView);
  64.  
  65. // GridViewExampleAdapter gAdapter = new GridViewExampleAdapter(this);
  66. // Set an Adapter to the ListView
  67. adapter = new CustomListAdapter(this);
  68. gv.setAdapter(adapter);
  69.  
  70. // Set on item click listener to the ListView
  71. gv.setOnItemClickListener(new OnItemClickListener() {
  72.  
  73. // @Override
  74. // public void onItemClick(AdapterView<?> parent, View v, int pos,
  75. // long id) {
  76. @Override
  77. public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
  78. int pos = arg2;
  79.  
  80. // Display the zoomed in image in full screen
  81.  
  82.  
  83.  
  84. zoomImageFromThumb(arg1, adapter.getThumbId(pos)); <--this doesn't work
  85.  
  86. feedContent = mfeed.getItem(pos).getFeedUrl();
  87.  
  88. // Log.d(TAG,"Position Clicked ["+pos+"] with item id ["+feeds+"]");
  89. }
  90. });
  91.  
  92. // Set the Animation time form the android defaults
  93. mShortAnimationDuration = getResources().getInteger(android.R.integer.config_shortAnimTime);
  94. }
  95.  
  96. @Override
  97. public void onBackPressed() {
  98.  
  99. // do something on back.
  100. this.startActivity(new Intent(ListActivity.this, MainActivity.class));
  101. finish();
  102.  
  103. return;
  104. }
  105.  
  106. private void zoomImageFromThumb(final View thumbView, int imageResId) {
  107.  
  108. // If there's an animation in progress, cancel it immediately and
  109. // proceed with this one.
  110. if (mCurrentAnimator != null) {
  111. mCurrentAnimator.cancel();
  112. }
  113.  
  114. // Load the high-resolution "zoomed-in" image.
  115. final ImageView expandedImageView = (ImageView) findViewById(R.id.expanded_image);
  116. final Button button = (Button) findViewById(R.id.button);
  117.  
  118. expandedImageView.setImageResource(imageResId);// <-- this is original code that works with a drawable
  119. expandedImageView.setId(R.id.expanded_image);// <--I have tried this among other solutions
  120.  
  121. // Calculate the starting and ending bounds for the zoomed-in image.
  122. // This step
  123. // involves lots of math. Yay, math.
  124. final Rect startBounds = new Rect();
  125. final Rect finalBounds = new Rect();
  126. final Point globalOffset = new Point();
  127.  
  128. // The start bounds are the global visible rectangle of the thumbnail,
  129. // and the
  130. // final bounds are the global visible rectangle of the container view.
  131. // Also
  132. // set the container view's offset as the origin for the bounds, since
  133. // that's
  134. // the origin for the positioning animation properties (X, Y).
  135. thumbView.getGlobalVisibleRect(startBounds);
  136. findViewById(R.id.container).getGlobalVisibleRect(finalBounds, globalOffset);
  137. startBounds.offset(-globalOffset.x, -globalOffset.y);
  138. finalBounds.offset(-globalOffset.x, -globalOffset.y);
  139.  
  140. // Adjust the start bounds to be the same aspect ratio as the final
  141. // bounds using the
  142. // "center crop" technique. This prevents undesirable stretching during
  143. // the animation.
  144. // Also calculate the start scaling factor (the end scaling factor is
  145. // always 1.0).
  146. float startScale;
  147.  
  148. if ((float) finalBounds.width() / finalBounds.height() > (float) startBounds.width() / startBounds.height()) {
  149.  
  150. // Extend start bounds horizontally
  151. startScale = (float) startBounds.height() / finalBounds.height();
  152.  
  153. float startWidth = startScale * finalBounds.width();
  154. float deltaWidth = (startWidth - startBounds.width()) / 2;
  155.  
  156. startBounds.left -= deltaWidth;
  157. startBounds.right += deltaWidth;
  158. } else {
  159.  
  160. // Extend start bounds vertically
  161. startScale = (float) startBounds.width() / finalBounds.width();
  162.  
  163. float startHeight = startScale * finalBounds.height();
  164. float deltaHeight = (startHeight - startBounds.height()) / 2;
  165.  
  166. startBounds.top -= deltaHeight;
  167. startBounds.bottom += deltaHeight;
  168. }
  169.  
  170. // Hide the thumbnail and show the zoomed-in view. When the animation
  171. // begins,
  172. // it will position the zoomed-in view in the place of the thumbnail.
  173. thumbView.setAlpha(0f);
  174. expandedImageView.setVisibility(View.VISIBLE);
  175. button.setVisibility(View.VISIBLE);
  176.  
  177. // Set the pivot point for SCALE_X and SCALE_Y transformations to the
  178. // top-left corner of
  179. // the zoomed-in view (the default is the center of the view).
  180. expandedImageView.setPivotX(0f);
  181. expandedImageView.setPivotY(0f);
  182.  
  183. // Construct and run the parallel animation of the four translation and
  184. // scale properties
  185. // (X, Y, SCALE_X, and SCALE_Y).
  186. AnimatorSet set = new AnimatorSet();
  187.  
  188. set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left,
  189. finalBounds.left)).with(ObjectAnimator.ofFloat(expandedImageView, View.Y,
  190. startBounds.top,
  191. finalBounds.top)).with(ObjectAnimator.ofFloat(expandedImageView,
  192. View.SCALE_X, startScale,
  193. 1f)).with(ObjectAnimator.ofFloat(expandedImageView,
  194. View.SCALE_Y, startScale, 1f));
  195. set.setDuration(mShortAnimationDuration);
  196. set.setInterpolator(new DecelerateInterpolator());
  197. set.addListener(new AnimatorListenerAdapter() {
  198. @Override
  199. public void onAnimationEnd(Animator animation) {
  200. mCurrentAnimator = null;
  201. }
  202. @Override
  203. public void onAnimationCancel(Animator animation) {
  204. mCurrentAnimator = null;
  205. }
  206. });
  207. set.start();
  208. mCurrentAnimator = set;
  209.  
  210. // Upon clicking the zoomed-in image, it should zoom back down to the
  211. // original bounds
  212. // and show the thumbnail instead of the expanded image.
  213. final float startScaleFinal = startScale;
  214.  
  215. button.setOnClickListener(new View.OnClickListener() {
  216. @Override
  217. public void onClick(View v) {
  218.  
  219. // TODO Auto-generated method stub
  220. thumbView.setAlpha(1f);
  221. expandedImageView.setVisibility(View.GONE);
  222. button.setVisibility(View.GONE);
  223. mCurrentAnimator = null;
  224. }
  225. });
  226. expandedImageView.setOnClickListener(new View.OnClickListener() {
  227. @Override
  228. public void onClick(View view) {
  229.  
  230. // if (mCurrentAnimator != null) {
  231. // Bundle bundle = new Bundle();
  232. // feedContent = imageAdapter.getItem(position);
  233. // System.out.println(feedContent);
  234. Intent intent = new Intent(ListActivity.this, SplashActivity.class);
  235.  
  236. intent.putExtra("feedContent", feedContent);
  237. startActivity(intent);
  238.  
  239. // finish();
  240.  
  241. // mCurrentAnimator.cancel();
  242. // }
  243. // Animate the four positioning/sizing properties in parallel,
  244. // back to their
  245. // original values.
  246. AnimatorSet set = new AnimatorSet();
  247.  
  248. set.play(ObjectAnimator.ofFloat(expandedImageView, View.X, startBounds.left)).with(
  249. ObjectAnimator.ofFloat(expandedImageView, View.Y, startBounds.top)).with(
  250. ObjectAnimator.ofFloat(expandedImageView, View.SCALE_X, startScaleFinal)).with(
  251. ObjectAnimator.ofFloat(expandedImageView, View.SCALE_Y, startScaleFinal));
  252. set.setDuration(mShortAnimationDuration);
  253. set.setInterpolator(new DecelerateInterpolator());
  254. set.addListener(new AnimatorListenerAdapter() {
  255. @Override
  256. public void onAnimationEnd(Animator animation) {
  257. thumbView.setAlpha(1f);
  258. expandedImageView.setVisibility(View.GONE);
  259. button.setVisibility(View.GONE);
  260. mCurrentAnimator = null;
  261. }
  262. @Override
  263. public void onAnimationCancel(Animator animation) {
  264. thumbView.setAlpha(1f);
  265. expandedImageView.setVisibility(View.GONE);
  266. button.setVisibility(View.GONE);
  267. mCurrentAnimator = null;
  268. }
  269. });
  270. set.start();
  271. mCurrentAnimator = set;
  272. }
  273. });
  274. }
  275.  
  276. // Create an Adapter Class extending the BaseAdapter
  277. class CustomListAdapter extends BaseAdapter {
  278. private LayoutInflater layoutInflater;
  279. public ImageLoader imageLoader;
  280.  
  281. public CustomListAdapter(ListActivity listActivity) {
  282. layoutInflater = (LayoutInflater) listActivity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  283. imageLoader = new ImageLoader(listActivity.getApplicationContext());
  284. }
  285.  
  286. @Override
  287. public int getCount() {
  288.  
  289. // Set the total list item count
  290. return mfeed.getItemCount();
  291. }
  292.  
  293. @Override
  294. public Object getItem(int position) {
  295. return position;
  296. }
  297. public int getThumbId(int position) {
  298. return gv.getId();
  299. }
  300. @Override
  301. public long getItemId(int position) {
  302. return position;
  303. }
  304.  
  305. @Override
  306. public View getView(int position, View convertView, ViewGroup parent) {
  307.  
  308. // Inflate the item layout and set the views
  309. ViewHolder view;
  310. View listItem = convertView;
  311. int pos = position;
  312.  
  313. if (listItem == null) {
  314. view = new ViewHolder();
  315. listItem = layoutInflater.inflate(R.layout.grid_item_anim_list, null);
  316. view.iv = (ImageView) listItem.findViewById(R.id.thumb_list);
  317.  
  318. // view.tv = (TextView) listItem.findViewById(R.id.title);
  319. listItem.setTag(view);
  320. } else {
  321. view = (ViewHolder) listItem.getTag();
  322. }
  323.  
  324. // Initialize the views in the layout
  325. // Set the views in the layout
  326. imageLoader.DisplayImage(mfeed.getItem(pos).getFeedImageUrl(), view.iv);
  327.  
  328. // view.tv.setText(feed.getItem(pos).getTitle());
  329. // tvDate.setText(feed.getItem(pos).getDate());
  330. return listItem;
  331. }
  332.  
  333. public class ViewHolder {
  334. public ImageView iv;
  335. public TextView tv;
  336. }
  337. }
  338. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement