ImageSwitcher褪色箭头下一个和上一个图像切换器按钮

时间:2011-05-16 19:15:40

标签: android function button android-widget android-image

我使用带有淡化箭头和缩放按钮的ImageSwitcher,唯一的问题是我无法设置按钮功能来更改图像。

以下是代码:

public class doloroso1 extends Activity implements
OnTouchListener, AdapterView.OnItemSelectedListener, ViewSwitcher.ViewFactory {

    private Integer[] mThumbIds = {
                R.drawable.d11, 
                R.drawable.d12,
                R.drawable.d13, 
                R.drawable.d14,
                R.drawable.d15, 
                R.drawable.d16,
                R.drawable.d17};


        private Integer[] mImageIds = {
                R.drawable.d11, 
                R.drawable.d12,
                R.drawable.d13, 
                R.drawable.d14,
                R.drawable.d15, 
                R.drawable.d16,
                R.drawable.d17};  

     View prev,next,zoom;
     Handler handler = new Handler();

      private void scheduleHideButtons() {
          handler.removeCallbacks(hideButtonsRunnable);
          handler.postDelayed(hideButtonsRunnable, 5000);
      }
      private Runnable hideButtonsRunnable = new Runnable() {
            @Override public void run() {
                fadeButtons(false);
            }  };     
            private void fadeButtons(final boolean fadeIn) {
                if (fadeIn) {
                    scheduleHideButtons();
                }
                Animation anim = AnimationUtils.loadAnimation(this, fadeIn?R.anim.fade_in:R.anim.fade_out);
                prev.startAnimation(anim);
                next.startAnimation(anim);
                zoom.startAnimation(anim);
                anim.setAnimationListener(new AnimationListener() {
                @Override
                public void onAnimationEnd(Animation animation) {
                        prev.setVisibility(fadeIn?View.VISIBLE:View.GONE);
                        next.setVisibility(fadeIn?View.VISIBLE:View.GONE);
                        zoom.setVisibility(fadeIn?View.VISIBLE:View.GONE);
                    }
                    @Override public void onAnimationRepeat(Animation animation) { }
                    @Override public void onAnimationStart(Animation animation) { }             
                });}


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.doloroso1);


       mSwitcher = (ImageSwitcher) findViewById(R.id.Imagen);
       mSwitcher.setFactory(this);
       mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
       mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));
        mSwitcher.setOnTouchListener(this);         
        prev = findViewById(R.id.anterior);        
        next = findViewById(R.id.siguiente);
        zoom = findViewById(R.id.zoom);
        scheduleHideButtons();



        Gallery g = (Gallery) findViewById(R.id.galeria);
        g.setAdapter(new ImageAdapter(this));
        g.setOnItemSelectedListener(this);

    }

        public void onItemSelected(AdapterView<?> parent, View v, int position, long id) {
            mSwitcher.setImageResource(mImageIds[position]);}


        public void onClickListener1(final int position, View convertView, ViewGroup parentparent, View v, int id) {
             next.setOnClickListener(new View.OnClickListener() {
                 public void onClick(View view) {
                   mSwitcher.setImageResource(mImageIds[position + 1]);}});           ;
        }


        public void onClickListener2(final int position, View convertView, ViewGroup parentparent, View v, int id) {
             prev.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View view) {
                      mSwitcher.setImageResource(mImageIds[position - 1]);}});}




    public void onNothingSelected(AdapterView<?> parent) {
    }

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

    private ImageSwitcher mSwitcher;

    public class ImageAdapter extends BaseAdapter {
        public ImageAdapter(Context c) {
            mContext = c;
        }

        public int getCount() {
            return mThumbIds.length;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView i = new ImageView(mContext);

            i.setImageResource(mThumbIds[position]);
            i.setAdjustViewBounds(true);
            i.setLayoutParams(new Gallery.LayoutParams(
                    LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            i.setBackgroundResource(R.styleable.galeria_android_galleryItemBackground);
            return i;
        }




    }



    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction()==MotionEvent.ACTION_DOWN) {
            if (prev.getVisibility()==View.GONE) {
                fadeButtons(true);
            }
            else {
                scheduleHideButtons();
            }
        }
        return false;}


    private Context mContext;
    }

1 个答案:

答案 0 :(得分:0)

我想你需要申报 ImageView i = new ImageView(mContext); 前 所以在你的onclick你可以打电话 i.setImageResource(索引);

您可能还需要创建索引变量并密切关注人们在数组中的位置