不仅实现了Lollipop中打开新的activity 的zoom效果(根据点击位置逐步展开过渡到新的界面),还实现了一个弹簧效果的ViewPager
public class MainActivity extends FragmentActivity {
private Context context;
private GridView gridView;
private ArrayList imgList = new ArrayList();
private ArrayList imgImageInfos = new ArrayList();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
imgList.add(0, R.drawable.p1);
imgList.add(1, R.drawable.p2);
imgList.add(2, R.drawable.p3);
imgList.add(3, R.drawable.p4);
imgList.add(4, R.drawable.p1);
imgList.add(5, R.drawable.p2);
imgList.add(6, R.drawable.p3);
imgList.add(7, R.drawable.p4);
imgList.add(8, R.drawable.p1);
gridView = (GridView) findViewById(R.id.gridview);
final ImageAdapter adapter = new ImageAdapter();
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView> parent, View view, int position, long id) {
Bundle bundle = new Bundle();
bundle.putIntegerArrayList("imgs", imgList);
bundle.putParcelable("info", ((PhotoView) view).getInfo());
bundle.putInt("position", position);
imgImageInfos.clear();
//NOTE:if imgList.size >= the visible count in single screen,i will cause NullPointException
//because item out of screen have been replaced/reused
for(int i = 0; i
import java.util.ArrayList;
import com.example.testimage.widget.ImageInfo;
import com.example.testimage.widget.PhotoView;
import com.example.testimage.widget.ReboundViewPager;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.TextView;
public class ViewPagerFragment extends Fragment{
private ReboundViewPager viewPager;
private TextView tips; //viewpager indicator
private ArrayList imgs;
private ImageInfo imageInfo;
private View mask;//background view
private ArrayList imageInfos;
private int position;
public static ViewPagerFragment getInstance(Bundle imgs){
ViewPagerFragment fragment = new ViewPagerFragment();
fragment.setArguments(imgs);
return fragment;
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_viewpager,null);
}
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
viewPager = (ReboundViewPager) view.findViewById(R.id.viewpager);
tips = (TextView) view.findViewById(R.id.text);
mask = view.findViewById(R.id.mask);
runEnterAnimation();
Bundle bundle = getArguments();
imgs = bundle.getIntegerArrayList("imgs");
imageInfo = bundle.getParcelable("info");
imageInfos = bundle.getParcelableArrayList("infos");
position = bundle.getInt("position", 0);
tips.setText((position + 1) + "/" + imgs.size());
viewPager.getOverscrollView().setAdapter(new PagerAdapter() {
@Override
public int getCount() {
return imgs.size();
}
@Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
@Override
public Object instantiateItem(ViewGroup container, int pos) {
PhotoView view = new PhotoView(getActivity());
view.touchEnable(true);
view.setImageResource(imgs.get(pos));
if(position == pos){//only animate when position equals u click in pre layout
view.animateFrom(imageInfo);
}
//force to get focal point,to listen key listener
view.setFocusableInTouchMode(true);
view.requestFocus();
view.setOnKeyListener(pressKeyListener);//add key listener to listen back press
view.setOnClickListener(onClickListener);
view.setTag(pos);
container.addView(view);
return view;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
});
viewPager.getOverscrollView().addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tips.setText((position + 1) + "/" + imgs.size());
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
//set current position
viewPager.getOverscrollView().setCurrentItem(position);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
exitFragment(v);
}
};
private void exitFragment(View v) {
//退出时点击的位置
int position = (int) v.getTag();
//回到上个界面该view的位置
runExitAnimation(v);
((PhotoView)v).animateTo(imageInfos.get(position), new Runnable() {
@Override
public void run() {
if (!ViewPagerFragment.this.isResumed()) {//fragment被回收
return;
}
final FragmentManager fragmentManager = getFragmentManager();
if (fragmentManager != null) {
fragmentManager.popBackStack();//回退栈
}
}
});
}
private View.OnKeyListener pressKeyListener = new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {//只监听返回键
if (event.getAction() != KeyEvent.ACTION_UP) {
return true;
}
exitFragment(v);
return true;
}
return false;
}
};
private void runEnterAnimation() {
AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
alphaAnimation.setDuration(300);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
mask.startAnimation(alphaAnimation);
}
public void runExitAnimation(final View view) {
AlphaAnimation alphaAnimation = new AlphaAnimation(1,0);
alphaAnimation.setDuration(300);
alphaAnimation.setInterpolator(new AccelerateInterpolator());
alphaAnimation.setAnimationListener(new Animation.AnimationListener() {
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationEnd(Animation animation) {
mask.setVisibility(View.GONE);
view.setVisibility(View.GONE);
}
@Override
public void onAnimationRepeat(Animation animation) {
}
});
mask.startAnimation(alphaAnimation);
}
}
- TestImage.rar (4.4 MB)
- 下载次数: 4
- 大小: 1.5 MB
- 大小: 643.3 KB
- 查看图片附件
服务器托管,北京服务器托管,服务器租用 http://www.fwqtg.net
机房租用,北京机房租用,IDC机房托管, http://www.e1idc.net