Achieve QQ space image preview effect, QQ space image Preview
Today, the project needs to preview images. Similar to the space for deduction, I have found too much code on the Internet, and many classes are comments everywhere. Then I wrote an image preview result, which is actually very simple.
First, let's analyze the implementation principle: Click the image to display a new window, slide to display the next one, and the page number also changes. Click it to exit (the code in this article is not available ); when you click an image to jump to the page, you should carry the current image page, then, you can use viewpager to load another page to display the current page. As for viewpager, you can use viewpager to display a monitoring event!
Import android. OS. Bundle;
Import android. support. v4.view. PagerAdapter;
Import android. support. v4.view. ViewPager;
Import android. view. LayoutInflater;
Import android. view. View;
Import android. view. ViewGroup;
Import android. widget. ImageView;
Import android. widget. TextView;
Import com. nostra13.universalimageloader. core. ImageLoader;
Import com. yusong. ycos. R;
Import com. yusong. ycos. wei_er.BaseActivity;
Import com. yusong. ycos. wei_er.config.AppConfig;
Import com. yusong. ycos. wei_er.view.DoubleScaleImageView;
Import java. util. ArrayList;
Import java. util. List;
/**
* Created by pcg on 2016/8/3.
*/
Public class ImagePreviewActivity extends BaseActivity implements ViewPager. OnPageChangeListener {
Private ViewPager viewPager;
Private TextView textView;
Private View view;
Private List <View> viewList = new ArrayList <View> ();
Private int position;
@ Override
Protected void onActivityCreate (Bundle savedInstanceState ){
SetContentView (R. layout. picture_view_pager );
(ImageView) findViewById (R. id. picture_back). setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Finish (); // This is the return key to exit
}
});
Position = getIntent (). getIntExtra ("position", 0 );
ViewPager = (ViewPager) findViewById (R. id. view_pager );
TextView = (TextView) findViewById (R. id. text _);
// Add a view, that is
For (int I = 0; I <ExceptionDetailActivity. list. size (); I ++ ){
View = LayoutInflater. from (this). inflate (R. layout. item_picture, null );
DoubleScaleImageView imageView = (DoubleScaleImageView) view. findViewById (R. id. picture _
String url = AppConfig. getInstance (). getIpAddress () + ExceptionDetailActivity. list. get (I). get (ExceptionDetailActivity. IMAGE );
ImageLoader. getInstance (). displayImage (url, imageView); // use the private message required by the image cache framework. I also have a lot of online messages after 576975755 deductions.
ViewList. add (view );
}
ViewPager. setAdapter (new MyAdapter ());
ViewPager. setCurrentItem (position); // you can specify the number of clicked images.
ViewPager. setOnPageChangeListener (this); // The page sliding listener is used to change the page number
// Predictiondetailactivity. list. size () Here is the url set for storing images list <Map <String, String>
TextView. setText (position + 1) + "/" + predictiondetailactivity. list. size ());
}
Class MyAdapter extends PagerAdapter {
@ Override
Public int getCount (){
Return viewList. size ();
}
@ Override
Public boolean isViewFromObject (View view, Object object ){
Return view = object;
}
@ Override
Public void destroyItem (ViewGroup container, int position, Object object Object ){
Container. removeView (viewList. get (position ));
}
@ Override
Public Object instantiateItem (ViewGroup container, int position ){
Container. addView (viewList. get (position), 0 );
Return viewList. get (position );
}
}
@ Override
Public void onPageScrolled (int position, float positionOffset, int positionOffsetPixels ){
}
@ Override
Public void onPageSelected (int position ){
// Change the page number
TextView. setText (position + 1) + "/" + predictiondetailactivity. list. size ());
}
@ Override
Public void onPageScrollStateChanged (int state ){
}
}
// Xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: background = "@ color/part_transparent">
<Android. support. v4.view. ViewPager
Android: id = "@ + id/view_pager"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"/>
<TextView
Android: id = "@ + id/text _"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_alignParentBottom = "true"
Android: layout_centerHorizontal = "true"
Android: text = "1/4"
Android: textColor = "@ color/white"
Android: textSize = "16sp"/>
<RelativeLayout
Android: layout_width = "fill_parent"
Android: layout_height = "40dp">
<ImageView
Android: id = "@ + id/picture_back"
Android: layout_width = "50dp"
Android: layout_height = "match_parent"
Android: padding = "15px"
Android: src = "@ drawable/back_click"/>
<TextView
Android: id = "@ + id/TV _top"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_centerInParent = "true"
Android: text = "abnormal image"
Android: textColor = "# FFFFFF"
Android: textSize = "18sp"/>
</RelativeLayout>
</RelativeLayout>
// Item xml Code
<? Xml version = "1.0" encoding = "UTF-8"?>
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical">
<RelativeLayout
Android: id = "@ + id/relative"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent">
<Com. yusong. ycos. wei_er.view.DoubleScaleImageView
Android: id = "@ + id/picture _"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: layout_centerVertical = "true"/>
</RelativeLayout>
</LinearLayout>
This is only a small number of images, which is quite 66. I don't know how many images will work (don't like it )!