0

我有全屏浏览图像的活动(比如画廊)。在这个活动中,我有离屏限制为 6 的 ViewPager。我将TouchImageView用于我的图像。问题是,当我第一次缩放图像并滑动到另一张照片时,我想在返回时看到未缩放的照片。TouchImageView 具有 resetZoom() 功能,但如何从 FragmentStatePageAdapter 或 ViewPager 获取屏幕外页面视图?这是我的活动代码:

public class ImageSlidesActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {

final int PAGE_LIMIT = 6;
final int PAGE_MARGIN = 8;

long userID;
long deviceID;
String key;
String[] mAttachments;

ImageSlidesAdapter mAdaper;
ViewPager mPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
    setContentView(R.layout.activity_image_slides);
    ActionBar actionBar = getActionBar();
    actionBar.setDisplayHomeAsUpEnabled(true);
    //actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#33ffffff")));

    Intent intent = getIntent();
    mAttachments = intent.getStringArrayExtra("attachments");
    int position = intent.getIntExtra("position", 0);

    SharedPreferences pref = getApplicationContext().getSharedPreferences("authData", 0);
    key = pref.getString("key", null);
    userID = pref.getLong("userID", 0);
    deviceID = pref.getLong("deviceID", 0);

    setPageIndicator(position);
    mAdaper = new ImageSlidesAdapter(getSupportFragmentManager(), mAttachments);

    final View container = findViewById(R.id.container);
    container.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            getActionBar().hide();
            container.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
        }
    });
    mPager = (ViewPager) findViewById(R.id.pager);
    mPager.setAdapter(mAdaper);
    mPager.setOnPageChangeListener(this);
    mPager.setOffscreenPageLimit(PAGE_LIMIT);
    mPager.setCurrentItem(position-1);

    /* cause bug with the white strip
    final float density = getResources().getDisplayMetrics().density;
    mPager.setPageMargin((int) (PAGE_MARGIN * density + 0.5f));
    mPager.setPageMarginDrawable(android.R.color.black);
    */
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    //getMenuInflater().inflate(R.menu.image_slides, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    if (id == android.R.id.home) {
        onBackPressed();
    }

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
    setPageIndicator(position+1);
}

@Override
public void onPageScrollStateChanged(int state) {

}

private void setPageIndicator(int page) {
    setTitle(Integer.toString(page) + "/" + Integer.toString(mAttachments.length));
}

/**
 * A fragment containing zoomable image.
 */
public static class ShowImageFragment extends Fragment {

    String mAlias;
    long mUserId;
    long mDeviceId;
    String mKey;
    TouchImageView mImage;

    static ShowImageFragment newInstance(String alias, long userId, long deviceId,
    String key) {
        ShowImageFragment f = new ShowImageFragment();

        Bundle args = new Bundle();
        args.putString("alias", alias);
        args.putLong("userID", userId);
        args.putLong("deviceID", deviceId);
        args.putString("key", key);
        f.setArguments(args);

        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle bundle = getArguments();
        mAlias = bundle.getString("alias");
        mUserId = bundle.getLong("userID");
        mDeviceId = bundle.getLong("deviceID");
        mKey = bundle.getString("key");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_image_slides, container, false);
        mImage = (TouchImageView) rootView.findViewById(R.id.imageView);
        final ProgressBar prBar = (ProgressBar) rootView.findViewById(R.id.progressBar);

        new AsyncTask<Void, Void, Bitmap>() {

            @Override
            protected Bitmap doInBackground(Void... voids) {
                try {
                    String[] links = API.getImageLinkByAlias(mAlias, mUserId,
                            mDeviceId, mKey);
                    return ImageLoader.getInstance().loadImageSync(links[1]);
                } catch (Exception e) {
                    // TODO: handle exceptions
                    e.printStackTrace();
                    cancel(true);
                    return null;
                }
            }

            @Override
            protected void onPostExecute(Bitmap bitmap) {
                prBar.setVisibility(View.GONE);
                if (bitmap == null) { return; }
                mImage.setImageBitmap(bitmap);
                mImage.setVisibility(View.VISIBLE);
                mImage.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (getActivity().getActionBar().isShowing()) {
                            getActivity().getActionBar().hide();
                        } else {
                            getActivity().getActionBar().show();
                        }
                    }
                });
            }
        }.execute();
        return rootView;
    }
}

public class ImageSlidesAdapter extends FragmentStatePagerAdapter {

    private String[] mAliases;
    public ImageSlidesAdapter(android.support.v4.app.FragmentManager fm, String[] aliases) {
        super(fm);
        mAliases = aliases;
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return ShowImageFragment.newInstance(mAliases[position], userID, deviceID, key);
    }

    @Override
    public int getCount() {
        return mAliases.length;
    }
}

}

4

3 回答 3

1

我重写了 onPageSelected 方法,并通过调用 getChildAt().

@Override
public void onPageSelected(int position) {
    setPageIndicator(position+1);
    if (position > 0) {
        View view = mPager.getChildAt(position -1);
        if (view != null) {
            TouchImageView img = (TouchImageView) view.findViewById(R.id.image);
            img.resetZoom();
        }
    }
    if (position < mPager.getChildCount() - 1) {
        View view = mPager.getChildAt(position + 1);
        if (view != null) {
            TouchImageView img = (TouchImageView) view.findViewById(R.id.image);
            img.resetZoom();
        }
    }
}
于 2014-11-02T15:29:01.700 回答
1

所以我为您的问题找到了另一种解决方案,在您的开关盒中

switch (event.getAction()) { }

我们需要再添加一个条件

case MotionEvent.ACTION_CANCEL:
     resetZoom();
     break;

这对我有用,我相信这对你也有用。

于 2015-10-21T23:01:49.760 回答
0

也可以通过在view pager中设置一个普通的page transformer来实现

binding.viewPager.setPageTransformer { page, position ->
        // Reset zoom on previous/next item
        if (position == -1f || position == 1f) {
            (page as PhotoView).apply {
                scale = 1f
            }
        }
    }
于 2022-02-21T17:03:58.267 回答