全屏图库代码

时间:2013-01-12 15:20:50

标签: android

我只是找到一个全屏幕库实现的解决方案,但我不能让代码工作。也许任何人都可以帮助我。

我需要一个没有缩略图库的全屏图像切换器,我可以用手指轻扫图片之间切换。

我收到以下错误: firstActivity无法解析为某种类型 slide_in_right无法解析或不是字段 slide_out_left无法解析或不是字段

我对Android编码很新:)

请帮帮我

package com.example.prog;

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.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.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.fr_l1_01,
R.drawable.fr_l1_02,
R.drawable.fr_l1_03,
R.drawable.fr_l1_04,
R.drawable.fr_l1_05
};

int curIndex=0;
int downX, upX;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mmenutest);

    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(firstActivity.this,android.R.anim.slide_in_left));
                    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(firstActivity.this,android.R.anim.slide_out_right));
                    //curIndex  current image index in array viewed by user
                    curIndex--;
                    if (curIndex < 0) {
                        curIndex = 5; //maximum
                    }

                    //imageList :-image list array
                    imageSwitcher.setImageResource(imageList[curIndex]);
                    //GalleryActivity.this.setTitle(curIndex);
                }
                else if (downX -upX > -100) {
                    curIndex++;
                    if (curIndex > 4) {
                        curIndex = 0;
                    }
                    imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,android.R.anim.slide_in_right));
                    imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,android.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

1 个答案:

答案 0 :(得分:1)

使用当前活动上下文来访问资源,而不是通过传递firstActivity.this来尝试访问动画资源的当前代码中的任何其他活动。

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));

使用l2_AltInvestActivity.this代替firstActivity.this