如何在 Android 中将类传递给 ImageSwitcher?

发布于 2024-11-30 07:19:45 字数 6019 浏览 0 评论 0原文

我正在使用 ImageSwitcher 来显示图片列表。当用户从 A.class 中的 Listview 中选择一个项目时,将调用 ImageSwitcher 并且用户查看图片。如果在列表中单击第 1 项,则应将一组特定的可绘制对象传递给图像切换器。如果单击列表中的第 2 项,则会显示一组不同的图片。我一直在努力解决如何将可绘制对象从 A.class 传递到 ImageSwitcher。

总之: 我想将一类可绘制对象从 A.class 传递到 ImageSwitcher,这样,如果单击项目 1,

private Integer[] mThumb = {R.Drawable.pic1, R.Drawable.pic2, R.Drawable.pic3}

则在 ImageSwitcher 中: 如果单击项目 2,我希望在 ImageSwitcher 中: private Integer[] mThumb = [R. Drawable.pic6, R.Drawable.pic7, R.Drawable.pic8}

从发送活动中,到目前为止我已经尝试了使用 putExtra 的许多排列putIntegralArrayExtra 等等。我确信这个问题的答案非常基本。我是java新手,并且仍在(缓慢)学习。感谢您的帮助!

这是图像切换器的代码,因此您可以明白我所说的 int[] mThumb 的含义:

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


    Matrix matrix = new Matrix();
    Matrix eventMatrix = new Matrix();

    final static int NONE = 0;
    final static int DRAG = 1;
    final static int ZOOM = 2;
    int touchState = NONE;




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

        setContentView(R.layout.imageswitcher);


        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));


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


    }


    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        mSwitcher.setImageResource(mImage[position]);
        final TextView tv = (TextView)findViewById(R.id.SwitcherText);
        tv.setText(mText[position]);    



    }  



    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setScaleType(ScaleType.MATRIX);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        i.setOnTouchListener(this);
        return i;
    }
    final static float MIN_DIST = 50;
    static float eventDistance = 0;
    static float centerX =0, centerY = 0;
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        //primary touch event starts: remember touch down location
        touchState = DRAG;
        centerX = event.getX(0);
        centerY = event.getY(0);
        eventMatrix.set(matrix);
        break;
        case MotionEvent.ACTION_POINTER_DOWN:
        //secondary touch event starts: remember distance and center
        eventDistance = calcDistance(event);
        calcMidpoint(centerX, centerY, event);
        if (eventDistance > MIN_DIST) {
        eventMatrix.set(matrix);
        touchState = ZOOM;
        }
        break;
        case MotionEvent.ACTION_MOVE:
            if (touchState == DRAG) {
            //single finger drag, translate accordingly
            matrix.set(eventMatrix);
            matrix.setTranslate(event.getX(0) - centerX,
            event.getY(0) - centerY);
            } else if (touchState == ZOOM) {
            //multi-finger zoom, scale accordingly around center
            float dist = calcDistance(event);
            if (dist > MIN_DIST) {
            matrix.set(eventMatrix);
            float scale = dist / eventDistance;
            matrix.postScale(scale, scale, centerX, centerY);
            }
            }
            // Perform the transformation
            i.setImageMatrix(matrix);
            break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
            touchState = NONE;
            break;
            }
            return true;
            }
            private float calcDistance(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
            }
            private void calcMidpoint(float centerX, float centerY,
            MotionEvent event) {
            centerX = (event.getX(0) + event.getX(1))/2;
            centerY = (event.getY(0) + event.getY(1))/2;
            }    


    private ImageSwitcher mSwitcher;

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

        public int getCount() {

        return mThumb.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 im = new ImageView(mContext);
            im.setImageResource(mThumb[position]);
            im.setAdjustViewBounds(true);
            im.setLayoutParams(new Gallery.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            im.setBackgroundResource(R.drawable.picture_frame);


            return im;
        }
        private Context mContext;
    }




  //these are the little pictures  
    private Integer[] mThumb = {
         R.drawable.lp_image1_thumb, R.drawable.lp_image2_thumb, R.drawable.lp_image3_thumb,
         R.drawable.lp_image4_thumb, R.drawable.lp_image5_thumb, R.drawable.lp_image6_thumb,
         R.drawable.lp_image7_thumb, R.drawable.lp_image8_thumb, R.drawable.lp_image9_thumb,
         R.drawable.lp_image10_thumb};

I am using an ImageSwitcher to display a list of pictures. When the user selects an item from a Listview in A.class, the ImageSwitcher is called and the user views the pictures. If item 1 is clicked on in the list, then a certain set of drawables should be passed to the imageswitcher. If item 2 on the list is clicked, a different set of pictures should be shown. I have been struggling with how to pass the drawables from A.class to the ImageSwitcher.

In Summary:
I want to pass a class of drawables from A.class to ImageSwitcher such that if item 1 is clicked, in ImageSwitcher:

private Integer[] mThumb = {R.Drawable.pic1, R.Drawable.pic2, R.Drawable.pic3}

and if item 2 is clicked, I want in ImageSwitcher for: private Integer[] mThumb = [R.Drawable.pic6, R.Drawable.pic7, R.Drawable.pic8}

From the sending Activity, so far I have tried many permutations of using putExtra putIntegralArrayExtra, etc etc. I'm sure the answer to this is pretty basic. I am new to java, and am still (slowly) learning. Thanks for your help!

Here is the code for the imageswitcher, so you can see what I mean by int[] mThumb:

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


    Matrix matrix = new Matrix();
    Matrix eventMatrix = new Matrix();

    final static int NONE = 0;
    final static int DRAG = 1;
    final static int ZOOM = 2;
    int touchState = NONE;




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

        setContentView(R.layout.imageswitcher);


        mSwitcher = (ImageSwitcher) findViewById(R.id.switcher);
        mSwitcher.setFactory(this);
        mSwitcher.setInAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_in));
        mSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this,
                android.R.anim.fade_out));


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


    }


    public void onItemSelected(AdapterView parent, View v, int position, long id) {
        mSwitcher.setImageResource(mImage[position]);
        final TextView tv = (TextView)findViewById(R.id.SwitcherText);
        tv.setText(mText[position]);    



    }  



    public void onNothingSelected(AdapterView parent) {
    }

    public View makeView() {
        ImageView i = new ImageView(this);
        i.setScaleType(ScaleType.MATRIX);
        i.setBackgroundColor(0xFF000000);
        i.setScaleType(ImageView.ScaleType.FIT_CENTER);
        i.setLayoutParams(new ImageSwitcher.LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
        i.setOnTouchListener(this);
        return i;
    }
    final static float MIN_DIST = 50;
    static float eventDistance = 0;
    static float centerX =0, centerY = 0;
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView) v;
        switch (event.getAction() & MotionEvent.ACTION_MASK) {
        case MotionEvent.ACTION_DOWN:
        //primary touch event starts: remember touch down location
        touchState = DRAG;
        centerX = event.getX(0);
        centerY = event.getY(0);
        eventMatrix.set(matrix);
        break;
        case MotionEvent.ACTION_POINTER_DOWN:
        //secondary touch event starts: remember distance and center
        eventDistance = calcDistance(event);
        calcMidpoint(centerX, centerY, event);
        if (eventDistance > MIN_DIST) {
        eventMatrix.set(matrix);
        touchState = ZOOM;
        }
        break;
        case MotionEvent.ACTION_MOVE:
            if (touchState == DRAG) {
            //single finger drag, translate accordingly
            matrix.set(eventMatrix);
            matrix.setTranslate(event.getX(0) - centerX,
            event.getY(0) - centerY);
            } else if (touchState == ZOOM) {
            //multi-finger zoom, scale accordingly around center
            float dist = calcDistance(event);
            if (dist > MIN_DIST) {
            matrix.set(eventMatrix);
            float scale = dist / eventDistance;
            matrix.postScale(scale, scale, centerX, centerY);
            }
            }
            // Perform the transformation
            i.setImageMatrix(matrix);
            break;
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_POINTER_UP:
            touchState = NONE;
            break;
            }
            return true;
            }
            private float calcDistance(MotionEvent event) {
            float x = event.getX(0) - event.getX(1);
            float y = event.getY(0) - event.getY(1);
            return FloatMath.sqrt(x * x + y * y);
            }
            private void calcMidpoint(float centerX, float centerY,
            MotionEvent event) {
            centerX = (event.getX(0) + event.getX(1))/2;
            centerY = (event.getY(0) + event.getY(1))/2;
            }    


    private ImageSwitcher mSwitcher;

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

        public int getCount() {

        return mThumb.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 im = new ImageView(mContext);
            im.setImageResource(mThumb[position]);
            im.setAdjustViewBounds(true);
            im.setLayoutParams(new Gallery.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
            im.setBackgroundResource(R.drawable.picture_frame);


            return im;
        }
        private Context mContext;
    }




  //these are the little pictures  
    private Integer[] mThumb = {
         R.drawable.lp_image1_thumb, R.drawable.lp_image2_thumb, R.drawable.lp_image3_thumb,
         R.drawable.lp_image4_thumb, R.drawable.lp_image5_thumb, R.drawable.lp_image6_thumb,
         R.drawable.lp_image7_thumb, R.drawable.lp_image8_thumb, R.drawable.lp_image9_thumb,
         R.drawable.lp_image10_thumb};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自由如风 2024-12-07 07:19:45

这个想法是你必须为列表视图中选定的项目创建一个索引(你可以使用listview.selectedItem)并实现侦听器来进行图像切换。

当用户从 A.class 中的 Listview 中选择一个项目时,将调用 ImageSwitcher...如果在列表中单击项目 1,则应将一组特定的可绘制对象传递给图像切换器。

实现ListView.setOnItemSelectedListener。你已经拥有了。

用户查看图片

实现OnTouchListener。你也已经有了它,但你必须添加图像切换。您可以使用滑动技术切换它,然后检查索引以了解您必须加载哪一组图片。只需为所选图片集中的当前图像提供 INDEX2 即可。

更新您的代码,然后我会指导您。

The idea is you have to create an INDEX for the selected item from the listview (you may use listview.selectedItem) and implement listener to do the image switching.

When the user selects an item from a Listview in A.class, the ImageSwitcher is called....If item 1 is clicked on in the list, then a certain set of drawables should be passed to the imageswitcher.

implement ListView.setOnItemSelectedListener. You have it already.

the user views the pictures

implement OnTouchListener. You have it also already but you have to add the image switching. You may switch it using sliding techniques then check the INDEX to know on what set of pictures you have to load. Just provide also a INDEX2 for the current image within the selected set of pictures.

update your code then I'll guide you.

╰ゝ天使的微笑 2024-12-07 07:19:45

这是我的做法,它可以工作,尽管它可能相当笨重:

发送活动:

if(position == 0)
{
int[] mThumb =
 {
 R.drawable.lp_image1,R.drawable.lp_image2, R.drawable.lp_image3, R.drawable.lp_image4,R.drawable.lp_image5, R.drawable.lp_image6, R.drawable.lp_image7,R.drawable.lp_image8,R.drawable.lp_image9
    }; 
myIntent = new Intent(view.getContext(), LP.class);
b.putIntArray("mImage", mImage);
b.putInt("i", 0);
}

接收活动:

Bundle b=this.getIntent().getExtras();
int[] mThumb = b.getIntArray("mThumbSent");
ImageAdapter adapter = new ImageAdapter(this, mThumb);

可以在任何需要的地方重复此代码,以从发送活动中引入 int[]

Here's how I did it, and it works, though it may be rather clunky:

sending activity:

if(position == 0)
{
int[] mThumb =
 {
 R.drawable.lp_image1,R.drawable.lp_image2, R.drawable.lp_image3, R.drawable.lp_image4,R.drawable.lp_image5, R.drawable.lp_image6, R.drawable.lp_image7,R.drawable.lp_image8,R.drawable.lp_image9
    }; 
myIntent = new Intent(view.getContext(), LP.class);
b.putIntArray("mImage", mImage);
b.putInt("i", 0);
}

Receiving Activity:

Bundle b=this.getIntent().getExtras();
int[] mThumb = b.getIntArray("mThumbSent");
ImageAdapter adapter = new ImageAdapter(this, mThumb);

And this code can be repeated wherever necessary to bring in the int[] from the sending activity

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文