PhotoView的用法

今天开发中,要对图片进行处理,然后我用到了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;


	@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文件
<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 {
	@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代码
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>

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 {


	@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>



  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 PhotoView 中实现涂鸦功能,你可以按照以下步骤进行操作: 1. 添加 PhotoView 依赖: 在你的项目的 `build.gradle` 文件中添加 PhotoView 依赖项: ```groovy dependencies { implementation 'com.github.chrisbanes:PhotoView:2.3.0' } ``` 确保使用的是最新版本的 PhotoView。 2. 在布局文件中添加 PhotoView: 在你的布局文件中添加 PhotoView 控件,并设置好相关属性,例如图片资源、缩放类型等: ```xml <com.github.chrisbanes.photoview.PhotoView android:id="@+id/photo_view" android:layout_width="match_parent" android:layout_height="match_parent" app:srcCompat="@drawable/your_image" /> ``` 3. 添加涂鸦功能: 在代码中获取 PhotoView 对象,并设置触摸事件监听器,以实现涂鸦功能。你可以使用 Canvas 和 Paint 类来绘制路径。 ```java PhotoView photoView = findViewById(R.id.photo_view); photoView.setOnPhotoTapListener(new PhotoViewAttacher.OnPhotoTapListener() { @Override public void onPhotoTap(ImageView view, float x, float y) { // 获取触摸点坐标,并将其转换为图片上的坐标 Drawable drawable = photoView.getDrawable(); if (drawable != null) { int drawableWidth = drawable.getIntrinsicWidth(); int drawableHeight = drawable.getIntrinsicHeight(); int viewWidth = photoView.getWidth(); int viewHeight = photoView.getHeight(); float imageX = (x * drawableWidth) / viewWidth; float imageY = (y * drawableHeight) / viewHeight; // 在图片上绘制涂鸦 Bitmap bitmap = Bitmap.createBitmap(drawableWidth, drawableHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); paint.setColor(Color.RED); paint.setStrokeWidth(5); canvas.drawPoint(imageX, imageY, paint); // 将修改后的图片显示在 PhotoViewphotoView.setImageBitmap(bitmap); } } }); ``` 在以上代码中,通过监听 `onPhotoTap` 方法获取触摸点的坐标,并将其转换为图片上的坐标。然后,使用 Canvas 和 Paint 绘制涂鸦,最后将修改后的图片显示在 PhotoView 上。 你可以根据需要修改涂鸦的样式、颜色和大小。 希望这些信息对你有所帮助!如果你有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值