【问题标题】:A “Before And After” Image Comparison Slide Control in AndroidAndroid 中的“前后”图像比较滑动控件
【发布时间】:2015-10-29 11:38:34
【问题描述】:

是否有可能在 Android 中构建一个图像比较滑动控件,就像 HTML 5 中存在的那样?

http://thenewcode.com/819/A-Before-And-After-Image-Comparison-Slide-Control-in-HTML5

我怎样才能做到这一点?

【问题讨论】:

  • 看来您只需要在 Slider 值更改时更改图像的 Alpha 值。
  • 看来你应该有 2 个位图(之前和之后)。我将创建一个自定义视图来保存/处理这两个位图并使用drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint) 相应地绘制它们。

标签: android photo


【解决方案1】:

我为此功能创建了library,可能是需要此功能的人,这对他们有好处。

compile 'com.github.developer--:beforeafterslider:1.0.4'

【讨论】:

  • 杰莫,干得好!我试过用你的滑块,真的很棒。虽然我发现了一个错误 - 当我为滑块的两个部分(之前和之后)设置相同的图像时,其中一个图像似乎变得扭曲,就像它稍微放大了一样(抱歉在这里写,我没有Github 帐户)。
  • 这是 ClipDrawableProcessorTask.kt 中的行 val tmpBitmap = getScaledBitmap(theBitmap) .所以我评论了它,现在一切正常。 “之前”图像在右侧,“之后”在左侧。再次感谢您的工作,对我演示一些图像处理效果帮助很大。
【解决方案2】:

一种方法是使用位于原始图像顶部的搜索栏和框架布局。当您滑动搜索栏时,帧高度会调整,其中包含第二张图像。

自上而下显示代码

    private SeekBar seekBar;

protected void onCreate(Bundle savedInstanceState) {

添加

seekBar = (SeekBar) findViewById(R.id.seekBar1);

        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

            int progress = 0;

            @Override

            public void onProgressChanged(SeekBar seekBar, int progresValue, boolean fromUser) {
                FrameLayout target = (FrameLayout) findViewById(R.id.target);

                progress = progresValue;

                ViewGroup.LayoutParams lp = target.getLayoutParams();
                lp.height = progress;
                target.setLayoutParams(lp);

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

布局

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:showIn="@layout/activity_main" tools:context=".MainActivity">

    <ImageView
        android:id="@+id/image"
        android:layout_width="600dp"
        android:layout_height="300dp"
        android:src="@drawable/pug_color"/>
    <FrameLayout
        android:id="@+id/target"
        android:layout_width="600dp"
        android:layout_height="150dp">
        <ImageView
            android:id="@+id/imageb"
            android:layout_width="600dp"
            android:layout_height="300dp"
            android:layout_gravity="center_horizontal"
            android:src="@drawable/pug_bw"/>
    </FrameLayout>

    <SeekBar
        android:id="@+id/seekBar1"
        android:layout_below="@+id/image"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:progress="300"
        android:max="600" />
</RelativeLayout>

【讨论】:

    猜你喜欢
    • 2015-10-27
    • 2023-03-11
    • 1970-01-01
    • 2011-09-01
    • 2013-01-29
    • 1970-01-01
    • 2021-10-07
    • 2012-04-04
    相关资源
    最近更新 更多