Ris*_*pal 11 java android android-viewpager imagefilter snapchat
基本上,我重新问这个问题,但是在android上实现它.
我试图允许用户在静态图像上的过滤器之间滑动.这个想法是当滤镜在其上方滚动时图像保持原位.Snapchat最近发布了一个实现此功能的版本.这段视频显示了我想要在1:05完成的事情.
我尝试使用覆盖填充列表并使用onFling和onDraw进行分页,但是我丢失了动画.有没有办法可以用ViewPager完成?
编辑:根据要求,我提供了覆盖视图分页的实现.它使用位于图像视图顶部的透明png图像填充viewpager.此外,此代码在C#中,因为我使用的是Xamarin Android.对于那些不熟悉C#的人来说,它与Java非常相似
...
static List<ImageView> overlayList = new List<ImageView>();
...
public class OverlayFragmentAdapter : FragmentPagerAdapter
{
public OverlayFragmentAdapter(Android.Support.V4.App.FragmentManager fm) : base(fm)
{
}
public override int Count
{
get { return 5; } //hardcoded temporarily
}
public override Android.Support.V4.App.Fragment GetItem(int position)
{
return new OverlayFragment ();
}
}
public class OverlayFragment : Android.Support.V4.App.Fragment
{
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.Inflate (Resource.Layout.fragment_overlay, container, false);
LinearLayout l1 = view.FindViewById<LinearLayout> (Resource.Id.overlay_container);
ImageView im = new ImageView (Activity);
im.SetImageResource (Resource.Drawable.Overlay); //Resource.Drawable.Overlay is a simple png transparency I created. R
l1.AddView (im);
overlayList.AddElement (im);
return view;
}
}
Run Code Online (Sandbox Code Playgroud)
活动布局XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom">
<ImageView
android:id="@+id/background_image"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout <!-- This second layout is for buttons which I have omitted from this code -->
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="@+id/edit_layout">
<android.support.v4.view.ViewPager
android:id="@+id/overlay_pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)
片段覆盖XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/overlay_container"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" />
Run Code Online (Sandbox Code Playgroud)
简要总结一下:viewpager位于第一个imageview的顶部,后者充当背景.OnCreateView方法从资源创建叠加片段和叠加图像视图,它放在overlay_container布局中.保存图像(我没有发布,因为它超出了这个问题的范围)很简单,它只是创建一个背景位图,一个覆盖位图,并使用画布将叠加层绘制到背景上,然后写入文件.
我自己也做过类似的事情。
对于您的特定用例,我将仅使用画布和 alpha 混合滤镜,在快速滑动时作为顶部图像。
要进行 Alpha 混合,请将第一个图像(原始图像)的 Alpha 绘画设置为 255,将第二个图像(滤镜)的 Alpha 绘画设置为 128 之类的值。
您只需要一个具有图像大小的过滤器,然后在绘制第二个图像时移动它的位置。就是这样。
它速度非常快,并且在非常非常旧的设备上运行得很好。
这是一个示例实现:
Bitmap filter, // the filter
original, // our original
tempBitmap; // the bitmap which holds the canvas results
// and is then drawn to the imageView
Canvas mCanvas; // our canvas
int x = 0; // The x coordinate of the filter. This variable will be manipulated
// in either onFling or onScroll.
void draw() {
// clear canvas
mCanvas.drawColor(Color.TRANSPARENT, PorterDuff.Mode.CLEAR);
// setup paint
paint0.setAlpha(255); // the original needs to be fully visible
paint1.setAlpha(128); // the filter should be alpha blended into the original.
// enable AA for paint
// filter image
paint1.setAntiAlias(true);
paint1.setFlags(Paint.ANTI_ALIAS_FLAG); // Apply AA to the image. Optional.
paint1.setFlags(Paint.FILTER_BITMAP_FLAG); // In case you scale your image, apple
// bilinear filtering. Optional.
// original image
paint0.setAntiAlias(true);
paint0.setFlags(Paint.ANTI_ALIAS_FLAG);
paint0.setFlags(Paint.FILTER_BITMAP_FLAG);
// draw onto the canvas
mCanvas.save();
mCanvas.drawBitmap(original, 0,0,paint0);
mCanvas.drawBitmap(filter, x,0,paint1);
mCanvas.restore();
// set the new image
imageView.setImageDrawable(new BitmapDrawable(getResources(), tempBitmap));
}
Run Code Online (Sandbox Code Playgroud)
private static final int SWIPE_DISTANCE_THRESHOLD = 125;
private static final int SWIPE_VELOCITY_THRESHOLD = 75;
// make sure to have implemented GestureDetector.OnGestureListener for these to work.
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
float distanceX = e2.getX() - e1.getX();
float distanceY = e2.getY() - e1.getY();
if (Math.abs(distanceX) > Math.abs(distanceY) && Math.abs(distanceX) >
SWIPE_DISTANCE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
// change picture to
if (distanceX > 0) {
// start left increment
}
else { // the left
// start right increment
}
}
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// checks if we're touching for more than 2f. I like to have this implemented, to prevent
// jerky image motion, when not really moving my finger, but still touching. Optional.
if (Math.abs(distanceY) > 2 || Math.abs(distanceX) > 2) {
if(Math.abs(distanceX) > Math.abs(distanceY)) {
// move the filter left or right
}
}
}
Run Code Online (Sandbox Code Playgroud)
注意:onScroll/onFling 实现具有用于 x 调整的伪代码,因为这些函数需要进行测试。将来最终实现此功能的人可以随意编辑答案并提供这些功能。
| 归档时间: |
|
| 查看次数: |
1596 次 |
| 最近记录: |