根据ImageSwitcher进度更新进度条

时间:2013-01-12 17:01:41

标签: android android-progressbar imageswitcher

我目前在我的应用中实现了ImageSwitcher,它可以滑动一系列图片。现在我希望在图片下方有一个ProgressBar,显示我们在集合中的哪个位置,这样用户就能感觉到剩下多少图片。

这可能吗?我可以就如何实现这个提出一些建议吗?

照片:

进展 [ - | --------]

我的ImageSwticher

package com.example.cfaslides;

import android.app.Activity;
 import android.os.Bundle;
 import android.view.View;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.ViewSwitcher.ViewFactory;
import android.util.Log;
import android.view.MotionEvent;
import android.widget.ProgressBar;
import android.widget.ViewSwitcher;
import android.widget.Gallery;
import android.widget.Gallery.LayoutParams;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.view.Window;
import android.content.Context;


public class l2_AltInvestActivity extends Activity implements ViewFactory {


ImageSwitcher imageSwitcher;

Integer[] imageList = {
R.drawable.av_01,
R.drawable.av_02,
R.drawable.av_03,
R.drawable.av_04,
R.drawable.av_05
};

int curIndex=0;
int maxIndex = 4; //# imageList -1
int downX, upX;

private Animation mIn1, mOut1, mIn2, mOut2;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.slider);
    final ProgressBar mProgressBar = (ProgressBar) findViewById(R.id.progressB);

mProgressBar.setProgress(0);
    mProgressBar.setMax(maxIndex);
    mProgressBar.setVisibility(View.VISIBLE);

     mIn1 =   AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left);
     mOut1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right);
     mIn2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right);
     mOut2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left);

     AnimationListener mAnimListener = new AnimationListener() {
         public void onAnimationEnd(Animation animation) {
                // the in animation has ended so update the ProgressBar with the new 
                // progress
                mProgressBar.setProgress(curIndex); // I don't know your progress?!? 
            }   
         @Override
          public void onAnimationStart(Animation animation) {
           // TODO Auto-generated method stub

          }

          @Override
          public void onAnimationRepeat(Animation animation) {
           // TODO Auto-generated method stub

          }
             // rest of the callbacks
     };  
     //set this listener for the both of the in animations 
     mIn1.setAnimationListener(mAnimListener);  
     mIn2.setAnimationListener(mAnimListener);
     // rest of the onCreate method


    imageSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
    imageSwitcher.setFactory(this);
    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_in));
    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.R.anim.fade_out));
    imageSwitcher.setImageResource(imageList[curIndex]);
    imageSwitcher.setOnTouchListener(new OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                downX = (int) event.getX();
                Log.i("event.getX()", " downX " + downX);
                return true;
            }
            else if (event.getAction() == MotionEvent.ACTION_UP) {
                upX = (int) event.getX();
                Log.i("event.getX()", " upX " + downX); 

                if (upX - downX > 100) {
                    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left));
                    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right));
                    //curIndex  current image index in array viewed by user
                    curIndex--;
                    if (curIndex < 0) {
                        curIndex = maxIndex; //maximum
                    }

                    //imageList :-image list array
                    imageSwitcher.setImageResource(imageList[curIndex]);
                    //GalleryActivity.this.setTitle(curIndex);
                }
                else if (downX -upX > -100) {
                    curIndex++;
                    if (curIndex > maxIndex) {
                        curIndex = 0;
                    }
                    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right));
                    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left));
                    imageSwitcher.setImageResource(imageList[curIndex]);
                    //GalleryActivity.this.setTitle(curIndex);
                }
            return true;
            }
        return false;
        }
    });
} //END onCreate

@Override
public View makeView() {
    ImageView i = new ImageView(this);
    i.setScaleType(ImageView.ScaleType.FIT_CENTER);
    i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
    i.setBackgroundColor(0xFF000000);
    return i;   
} //END makeView

} // END Class

滑块:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="@+id/widget32"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ProgressBar
    android:id="@+id/progressB"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<ImageSwitcher android:id="@+id/switcher"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
>

</ImageSwitcher>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

  

现在我想在图片下面显示一个进度条   我们在集合中的位置。所以有人得到了   感觉剩下多少照片。

  1. ProgressBar布局文件
  2. 中放置R.layout.slider
  3. 将用于Animations转换的四个ImageSwitcher变为Activity中的字段,并为每个AnimationListener设置//... private Animation mIn1, mOut1, mIn2, mOut2; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.slider); final ProgressBar = (ProgressBar) findViewById(R.id.theIdOfTheProgressBar); mIn1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_in_left); mOut1 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,android.R.anim.slide_out_right); mIn2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_in_right); mOut2 = AnimationUtils.loadAnimation(l2_AltInvestActivity.this,R.anim.slide_out_left); AnimationListener mAnimListener = new AnimationListener() { public void onAnimationEnd(Animation animation) { // the in animation has ended so update the ProgressBar with the new // progress mProgressBar.setProgress(curIndex); // I don't know your progress?!? } // rest of the callbacks }); //set this listener for the both of the in animations mIn1.setAnimationListener(mAnimListener); mIn2.setAnimationListener(mAnimListener); // rest of the onCreate method

    onTouch
  4. ImageSwitcher方法中使用正确的动画更新mIn1(来自mOut1mIn2mOut2,{{1}} )