PhotoView的用法

来源:互联网 发布:联想e41指纹识别软件 编辑:程序博客网 时间:2024/05/20 19:45
今天开发中,要对图片进行处理,然后我用到了PhotoView ,这里就记录下用法。


项目地址: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%90
Demo地址: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;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_fullscreen);mPhotoView = (PhotoView) findViewById(R.id.photoview);mPhotoView.setImageResource(R.drawable.saya);}}

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>


2.ViewPagerDemo

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 {@Overrideprotected 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 {@Overridepublic int getCount() {return 3;}@Overridepublic void destroyItem(ViewGroup container, int position, Object object) {container.removeView((View) object);}@Overridepublic boolean isViewFromObject(View view, Object object) {return view == object;}@Overridepublic 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代码
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 {@Overrideprotected 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>

4.PicassoDemo
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 {@Overrideprotected 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>



2 0
原创粉丝点击