一个用于多个Imageview布局的ImageView对象

时间:2014-01-09 01:58:15

标签: android view imageview

我可以使用一个具有多个Imageview布局的ImageView对象吗?我有4个不同的XML文件,每个文件都有一个imageview标签,在不同的用户条件下充气。但在我的代码中,我想只使用一个ImageView对象来动态分配图像资源。但我无法做到这一点,并且出于某种原因,即使在为每个膨胀的布局执行imgview =(ImageView)findViewbyId(R.mylayouts.customImage)之后,它仍然显示与之关联的第一个图像资源。请帮忙。下面是我正在使用的寻呼机视图的公共Object instantiateItem的代码。

public Object instantiateItem(View pager, int position) {
        View v = null;
        if (position == 0) {
            v = mInflater.inflate(R.layout.mylayout1, null);
        } else if (position == 1) {
            v = mInflater.inflate(R.layout.mylayout2, null);

            if (mHelpImages1 == null) {  //int array to store image resourceID
                mHelpImages1 = new int[] { R.drawable.imageB1, R.drawable.imageB2 };
            }
            mImage1 = null;  //my imageView which i want to use everywhere
            mImage1 = (ImageView) v.findViewById(R.id.imageView1);
        } else if (position == 2) {
            v = mInflater.inflate(R.layout.myLayout5, null);

            if (mHelpImages2 == null) {
                mHelpImages2 = new int[] { R.drawable.imageB3,
                        R.drawable.imageB4 };
            }

            String strNavi = getString(R.string.myString1) + "\n" + getString(R.string.myString2);
            mImage1 = null;
            mImage1 = (ImageView) v.findViewById(R.id.myLayout3);
            mDesc2 = (TextView) v.findViewById(R.id.myString3);
            mDesc2.setText(strNavi);
        } else if (position == 3) {
            v = mInflater.inflate(R.layout.myLayout6, null);
            TextView tv = (TextView) v.findViewById(R.id.textView4);
            tv.setSelected(true);
            tv.setSingleLine();
            tv.setEllipsize(TextUtils.TruncateAt.MARQUEE);

            if (mHelpImages3 == null) {
                mHelpImages3 = new int[] { R.drawable.imageB7,
                        R.drawable.imageB8, };
            }
            mImage1 = null;
            mImage1 = (ImageView) v.findViewById(R.id.imageView8);
        } 


        ScrollView scrollview = (ScrollView) v.findViewById(R.id.scrollview);
        try {
            if (scrollview != null) {
                scrollview.updateCustomEdgeGlow(getResources().getDrawable(R.drawable.edge),
                        getResources().getDrawable(R.drawable.edge2));
            }
        } catch (NoSuchMethodError e) {
            Log.e(TAG, "updateCustomEdgeGlow ERROR");
        } catch (Exception e) {
            Log.e(TAG, "updateCustomEdgeGlow Exception");
            e.printStackTrace();
        }

        ((ViewPager) pager).addView(v, 0);
        return v;
    }
  

在onPageSelected我这样做

  public void onPageSelected(int position) {
    mPrevPosition = position;
    if (mHandler != null && mHandler.hasMessages(myMessage)) {
        mHandler.removeMessages(myMessage);
    }

    mSceneCount = 0;

    if (position != 0) {
        mHandler.sendEmptyMessageDelayed(myMessage, 0);
    }
}
  

并且处理程序每​​隔3秒调用另一个函数changeImage,它会像这样更改imageView的imageResource

private void changeImage() {

    if (mSceneCount == 100) {
        mSceneCount = 0;
    }

    switch (mPrevPosition) {
    case 1:
        if (mHelpImages1 != null) {
            int index1 = mSceneCount % mHelpImages1.length;
            Drawable mm = new BitmapDrawable(getResources(),
                    mBitmap.decodeSampledBitmapFromResource(getResources(), mHelpImages1[index1],
                            mImage1.getWidth(), mImage1.getHeight()));
            mImage1.setBackground(null);
            mImage1.destroyDrawingCache();
            mImage1.setBackground(mm);
            mm = null;
        }
        break;

        // and other switchcases here, but basically this is how i do it.


              default:
        break;
    }

    mImageCount++;
}
  

如果我使用多个ImageView对象,即一个用于一个布局的ImageView,代码就可以正常工作。

0 个答案:

没有答案