在build.gradle中添加依赖:
implementation 'com.github.pavel163:BifacialView:1.4.1'
在布局文件中:
<com.ebr163.bifacialview.view.BifacialViewandroid:id="@+id/bv_before_after"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_centerInParent="true"app:arrowColor="@color/colorWhite"app:arrowCornerRadius="2dp"app:arrowFill="true"app:arrowHeight="12dp"app:arrowMargin="3dp"app:arrowStrokeWidth="2dp"app:arrowVisibility="true"app:arrowWidth="8dp"app:delimiterPadding="10dp"app:delimiterWidth="1dp"app:drawableLeft="@mipmap/home_3de_defaultcover"app:drawableRight="@mipmap/home_3de_defaultcover" />
home_3de_defaultcover 是随便搞个默认图片,drawableLeft是左边的图片,drawableRight是右边的。
代码中更新图片:
1、加载网络图片:
//异步任务把前一张网络图片转成drawable资源private class DownloadImageTask extends AsyncTask<String, Void, Drawable>{protected Drawable doInBackground(String... urls) {return loadImageFromNetwork(urls[0],"beforeImageWYCN.jpg");}protected void onPostExecute(Drawable result) {bv_before_after.setDrawableLeft(result);}}//异步任务把后一张网络图片转成drawable资源private class DownloadImageTaskAfter extends AsyncTask<String, Void, Drawable>{protected Drawable doInBackground(String... urls) {return loadImageFromNetwork(urls[0],"afterImageWYCN.jpg");}protected void onPostExecute(Drawable result) {bv_before_after.setDrawableRight(result);}}//调用方法:
new DownloadImageTaskAfter().execute(afterImage) ;
new DownloadImageTask().execute(beforeImage) ;
2、加载本地bitmap:
BitmapDrawable bd=new BitmapDrawable(bmBefore);bv_before_after.setDrawableLeft(bd);BitmapDrawable bdAfter=new BitmapDrawable(bmAfter);bv_before_after.setDrawableRight(bdAfter);
用到的控件的github项目地址:GitHub - pavel163/BifacialView