转载自:http://blog.csdn.net/fingerth/article/details/50477112
项目地址:https://github.com/chrisbanes/PhotoView
原理剖析文档:http://a.codekk.com/detail/Android/dkmeteor/PhotoView%20%E6%BA%90%E7%A0%81%E8%A7%A3%E6%9E%90Demo地址:https://raw.githubusercontent.com/android-cn/android-open-project-demo/master/photoview-demo/app/photoview-demo.apk
Demo中PhotoView的用法。用法比较简单,直接代码解释。
1.FullScreenDemo
java代码
- import uk.co.senab.photoview.PhotoView;
- import android.app.Activity;
- import android.os.Bundle;
- public class FullScreenDemo extends Activity {
- private PhotoView mPhotoView;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_fullscreen);
- mPhotoView = (PhotoView) findViewById(R.id.photoview);
- mPhotoView.setImageResource(R.drawable.saya);
- }
- }
xml文件
2.ViewPagerDemo
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <uk.co.senab.photoview.PhotoView
- android:id="@+id/photoview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
Java代码
- import uk.co.senab.photoview.PhotoView;
- import android.app.Activity;
- import android.os.Bundle;
- import android.support.v4.view.PagerAdapter;
- import android.support.v4.view.ViewPager;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewGroup.LayoutParams;
- public class ViewPagerDemo extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_viewpager);
- ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
- pager.setAdapter(new DemoAdapter());
- }
- class DemoAdapter extends PagerAdapter {
- @Override
- public int getCount() {
- return 3;
- }
- @Override
- public void destroyItem(ViewGroup container, int position, Object object) {
- container.removeView((View) object);
- }
- @Override
- public boolean isViewFromObject(View view, Object object) {
- return view == object;
- }
- @Override
- public Object instantiateItem(ViewGroup container, int position) {
- PhotoView photoview = new PhotoView(ViewPagerDemo.this);
- photoview.setImageResource(R.drawable.saya);
- container.addView(photoview, LayoutParams.MATCH_PARENT,
- LayoutParams.MATCH_PARENT);
- return photoview;
- }
- }
- }
xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <android.support.v4.view.ViewPager
- android:id="@+id/viewpager"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
3.ImageLoaderDemo
java代码
4.PicassoDemo
- import com.nostra13.universalimageloader.core.ImageLoader;
- import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
- import uk.co.senab.photoview.PhotoView;
- import android.app.Activity;
- import android.os.Bundle;
- public class ImageLoaderDemo extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_integration);
- PhotoView photoView = (PhotoView) findViewById(R.id.photoview);
- ImageLoader.getInstance().init(
- ImageLoaderConfiguration.createDefault(this));
- /**
- * The Image URL is comes from my space. If it is invalid, you can
- * change to any other picture url.
- */
- ImageLoader.getInstance().displayImage(
- "http://img5.imgtn.bdimg.com/it/u=854933491,3774339278&fm=21&gp=0.jpg", photoView);
- }
- }
xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <uk.co.senab.photoview.PhotoView
- android:id="@+id/photoview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>
java代码
- import uk.co.senab.photoview.PhotoView;
- import android.app.Activity;
- import android.os.Bundle;
- import android.util.DisplayMetrics;
- import com.squareup.picasso.Picasso;
- public class PicassoDemo extends Activity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_integration);
- PhotoView photoView = (PhotoView) findViewById(R.id.photoview);
- DisplayMetrics dm = new DisplayMetrics();
- getWindowManager().getDefaultDisplay().getMetrics(dm);
- Picasso.with(this).load("http://img5.imgtn.bdimg.com/it/u=854933491,3774339278&fm=21&gp=0.jpg")
- .centerInside().resize(dm.widthPixels, dm.heightPixels).tag(this).into(photoView);
- }
- }
xml文件
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <uk.co.senab.photoview.PhotoView
- android:id="@+id/photoview"
- android:layout_width="match_parent"
- android:layout_height="match_parent" />
- </LinearLayout>