Imageswitcher Animation Problem

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

Imageswitcher Animation Problem

Postby sohrab » Wed Dec 09, 2009 9:18 am

Hi
I am new in Android application development. So far I have been testing the sample applications shipped with the Android SDK in the emulator. In the imageswitcher1 activity of the ApiDemos application I felt that the fade in animation and fadeout animation is running at the same time. I want that the current image will totally fade out first and then fade in will start. How will I do that?

Note: I have tried that application only in the emulator not in an android handset.

Code is following:


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2.  
  3.  
  4. /*
  5.  
  6.  * Copyright (C) 2007 The Android Open Source Project
  7.  
  8.  *
  9.  
  10.  * Licensed under the Apache License, Version 2.0 (the "License");
  11.  
  12.  * you may not use this file except in compliance with the License.
  13.  
  14.  * You may obtain a copy of the License at
  15.  
  16.  *
  17.  
  18.  *      http://www.apache.org/licenses/LICENSE-2.0
  19.  
  20.  *
  21.  
  22.  * Unless required by applicable law or agreed to in writing, software
  23.  
  24.  * distributed under the License is distributed on an "AS IS" BASIS,
  25.  
  26.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  27.  
  28.  * See the License for the specific language governing permissions and
  29.  
  30.  * limitations under the License.
  31.  
  32.  */
  33.  
  34.  
  35.  
  36. package com.example.android.apis.view;
  37.  
  38.  
  39.  
  40. import com.example.android.apis.R;
  41.  
  42.  
  43.  
  44. import android.app.Activity;
  45.  
  46. import android.content.Context;
  47.  
  48. import android.os.Bundle;
  49.  
  50. import android.view.View;
  51.  
  52. import android.view.ViewGroup;
  53.  
  54. import android.view.Window;
  55.  
  56. import android.view.animation.Animation;
  57.  
  58. import android.view.animation.AnimationUtils;
  59.  
  60. import android.widget.AdapterView;
  61.  
  62. import android.widget.BaseAdapter;
  63.  
  64. import android.widget.Gallery;
  65.  
  66. import android.widget.Gallery.LayoutParams;
  67.  
  68. import android.widget.ImageSwitcher;
  69.  
  70. import android.widget.ImageView;
  71.  
  72. import android.widget.ViewSwitcher;
  73.  
  74.  
  75.  
  76.  
  77.  
  78. public class ImageSwitcher1 extends Activity implements
  79.  
  80.         AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {
  81.  
  82.  
  83.  
  84.     @Override
  85.  
  86.     public void onCreate(Bundle savedInstanceState) {
  87.  
  88.         super.onCreate(savedInstanceState);
  89.  
  90.         requestWindowFeature(Window.FEATURE_NO_TITLE);
  91.  
  92.  
  93.  
  94.         setContentView(R.layout.image_switcher_1);
  95.  
  96.  
  97.  
  98.         mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
  99.  
  100.         mSwitcher.setFactory(this);
  101.  
  102.        
  103.  
  104.         Animation inAnimation = AnimationUtils.loadAnimation(this, android.R.anim.fade_in);
  105.  
  106.         inAnimation.setDuration(500L);
  107.  
  108.        
  109.  
  110.         Animation outAnimation = AnimationUtils.loadAnimation(this,android.R.anim.fade_out);
  111.  
  112.         outAnimation.setDuration(500L);
  113.  
  114.  
  115.  
  116.         mSwitcher.setInAnimation(inAnimation);
  117.  
  118.         mSwitcher.setOutAnimation(outAnimation);
  119.  
  120.  
  121.  
  122.         Gallery g = (Gallery) findViewById(R.id.gallery);
  123.  
  124.         g.setAdapter(new ImageAdapter(this));
  125.  
  126.         g.setOnItemSelectedListener(this);
  127.  
  128.     }
  129.  
  130.  
  131.  
  132.     public void onItemSelected(AdapterView parent, View v, int position, long id) {
  133.  
  134.         mSwitcher.setImageResource(mImageIds[position]);
  135.  
  136.     }
  137.  
  138.  
  139.  
  140.     public void onNothingSelected(AdapterView parent) {
  141.  
  142.     }
  143.  
  144.  
  145.  
  146.     public View makeView() {
  147.  
  148.         ImageView i = new ImageView(this);
  149.  
  150.         i.setBackgroundColor(0xFF000000);
  151.  
  152.         i.setScaleType(ImageView.ScaleType.FIT_CENTER);
  153.  
  154.         i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,
  155.  
  156.                 LayoutParams.FILL_PARENT));
  157.  
  158.         return i;
  159.  
  160.     }
  161.  
  162.  
  163.  
  164.     private ImageSwitcher mSwitcher;
  165.  
  166.  
  167.  
  168.     public class ImageAdapter extends BaseAdapter {
  169.  
  170.         public ImageAdapter(Context c) {
  171.  
  172.             mContext = c;
  173.  
  174.         }
  175.  
  176.  
  177.  
  178.         public int getCount() {
  179.  
  180.             return mThumbIds.length;
  181.  
  182.         }
  183.  
  184.  
  185.  
  186.         public Object getItem(int position) {
  187.  
  188.             return position;
  189.  
  190.         }
  191.  
  192.  
  193.  
  194.         public long getItemId(int position) {
  195.  
  196.             return position;
  197.  
  198.         }
  199.  
  200.  
  201.  
  202.         public View getView(int position, View convertView, ViewGroup parent) {
  203.  
  204.             ImageView i = new ImageView(mContext);
  205.  
  206.  
  207.  
  208.             i.setImageResource(mThumbIds[position]);
  209.  
  210.             i.setAdjustViewBounds(true);
  211.  
  212.             i.setLayoutParams(new Gallery.LayoutParams(
  213.  
  214.                     LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
  215.  
  216.             i.setBackgroundResource(R.drawable.picture_frame);
  217.  
  218.             return i;
  219.  
  220.         }
  221.  
  222.  
  223.  
  224.         private Context mContext;
  225.  
  226.  
  227.  
  228.     }
  229.  
  230.  
  231.  
  232.     private Integer[] mThumbIds = {
  233.  
  234.             R.drawable.sample_thumb_0, R.drawable.sample_thumb_1,
  235.  
  236.             R.drawable.sample_thumb_2, R.drawable.sample_thumb_3,
  237.  
  238.             R.drawable.sample_thumb_4, R.drawable.sample_thumb_5,
  239.  
  240.             R.drawable.sample_thumb_6, R.drawable.sample_thumb_7};
  241.  
  242.  
  243.  
  244.     private Integer[] mImageIds = {
  245.  
  246.             R.drawable.sample_0, R.drawable.sample_1, R.drawable.sample_2,
  247.  
  248.             R.drawable.sample_3, R.drawable.sample_4, R.drawable.sample_5,
  249.  
  250.             R.drawable.sample_6, R.drawable.sample_7};
  251.  
  252.  
  253.  
  254. }
  255.  
  256.  
Parsed in 0.048 seconds, using GeSHi 1.0.8.4
sohrab
Once Poster
Once Poster
 
Posts: 1
Joined: Wed Dec 09, 2009 8:34 am
Location: Tokyo, Japan

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 7 guests