当我使用Gradle依赖时,如何覆盖函数?

时间:2016-01-08 02:04:31

标签: android

我在我的项目中使用range-seek-bar(https://github.com/anothem/android-range-seek-bar)控件,我希望格式化android-range-seek-bar的所选最小值和最大值的标签。 我浏览源代码,我想我应该更改RangeSeekBar.java中函数onDraw中的代码。我怎么能这样做?

顺便说一句,我不想​​创建一个新的基于范围搜索栏的项目并修改代码。

UI

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:rsb="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/border_ui">


    <LinearLayout
        android:id="@+id/linearLayoutToolBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        style="@style/myToolbar"
        android:weightSum="4" >

        <Button
            android:id="@+id/btnCrop"
            style="@style/myTextMedium"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="@string/BtnCrop" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/LinerSeekBar"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/linearLayoutToolBar"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:paddingBottom="14dp"
        android:paddingTop="1dp"
        android:layout_centerHorizontal="true"
        android:orientation="vertical">

           <org.florescu.android.rangeseekbar.RangeSeekBar
            android:id="@+id/myRangeSeekBar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            rsb:absoluteMaxValue="10000"
            rsb:absoluteMinValue="0"
            rsb:activateOnDefaultValues="true"
            rsb:showLabels="true"
            rsb:valuesAboveThumbs="false"
            rsb:textAboveThumbsColor="@color/black" />

    </LinearLayout>


    <TextureView
        android:id="@+id/mTextureView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"

        android:layout_marginBottom="5dp"
        android:layout_marginTop="10dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"

        android:layout_above="@+id/LinerSeekBar"
        android:layout_centerHorizontal="true"
     />


</RelativeLayout>

摇篮

apply plugin: 'com.android.application'

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.0"       

    productFlavors {
        free {
            applicationId "info.dodata.screenrecorder"
        }

        pro {
            applicationId "info.dodata.screenrecorder.pro"
        }
    }

    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'

            buildConfigField "boolean", "IsDebugMode", "false"
        }

        debug {
            buildConfigField "boolean", "IsDebugMode", "true"
        }
    }

}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.google.android.gms:play-services:7.3.0'
    compile 'com.android.support:support-v13:23.1.0'
    compile 'com.googlecode.mp4parser:isoparser:1.1.17'
    compile 'org.florescu.android.rangeseekbar:rangeseekbar-library:0.3.0'
}

RangeSeekBar.java

/**
     * Draws the widget on the given canvas.
     */
    @Override
    protected synchronized void onDraw(@NonNull Canvas canvas) {
        super.onDraw(canvas);

        paint.setTextSize(mTextSize);
        paint.setStyle(Style.FILL);
        paint.setColor(mDefaultColor);
        paint.setAntiAlias(true);
        float minMaxLabelSize = 0;

        if (mShowLabels) {
            // draw min and max labels
            String minLabel = getContext().getString(R.string.demo_min_label);
            String maxLabel = getContext().getString(R.string.demo_max_label);
            minMaxLabelSize = Math.max(paint.measureText(minLabel), paint.measureText(maxLabel));
            float minMaxHeight = mTextOffset + mThumbHalfHeight + mTextSize / 3;
            canvas.drawText(minLabel, 0, minMaxHeight, paint);
            canvas.drawText(maxLabel, getWidth() - minMaxLabelSize, minMaxHeight, paint);
        }
        padding = mInternalPad + minMaxLabelSize + mThumbHalfWidth;

        // draw seek bar background line
        mRect.left = padding;
        mRect.right = getWidth() - padding;
        canvas.drawRect(mRect, paint);

        boolean selectedValuesAreDefault = (getSelectedMinValue().equals(getAbsoluteMinValue()) &&
                getSelectedMaxValue().equals(getAbsoluteMaxValue()));

        int colorToUseForButtonsAndHighlightedLine = !mAlwaysActive && !mActivateOnDefaultValues && selectedValuesAreDefault ?
                mDefaultColor : // default values
                mActiveColor;   // non default, filter is active

        // draw seek bar active range line
        mRect.left = normalizedToScreen(normalizedMinValue);
        mRect.right = normalizedToScreen(normalizedMaxValue);

        paint.setColor(colorToUseForButtonsAndHighlightedLine);
        canvas.drawRect(mRect, paint);

        // draw minimum thumb (& shadow if requested) if not a single thumb control
        if (!mSingleThumb) {
            if (mThumbShadow) {
                drawThumbShadow(normalizedToScreen(normalizedMinValue), canvas);
            }
            drawThumb(normalizedToScreen(normalizedMinValue), Thumb.MIN.equals(pressedThumb), canvas,
                    selectedValuesAreDefault);
        }

        // draw maximum thumb & shadow (if necessary)
        if (mThumbShadow) {
            drawThumbShadow(normalizedToScreen(normalizedMaxValue), canvas);
        }
        drawThumb(normalizedToScreen(normalizedMaxValue), Thumb.MAX.equals(pressedThumb), canvas,
                selectedValuesAreDefault);

        // draw the text if sliders have moved from default edges
        if (mShowTextAboveThumbs && (mActivateOnDefaultValues || !selectedValuesAreDefault)) {
            paint.setTextSize(mTextSize);
            paint.setColor(mTextAboveThumbsColor);
            // give text a bit more space here so it doesn't get cut off
            int offset = PixelUtil.dpToPx(getContext(), TEXT_LATERAL_PADDING_IN_DP);

            String minText = String.valueOf(getSelectedMinValue());
            String maxText = String.valueOf(getSelectedMaxValue());
            float minTextWidth = paint.measureText(minText) + offset;
            float maxTextWidth = paint.measureText(maxText) + offset;

            if (!mSingleThumb) {
                canvas.drawText(minText,
                        normalizedToScreen(normalizedMinValue) - minTextWidth * 0.5f,
                        mDistanceToTop + mTextSize,
                        paint);

            }

            canvas.drawText(maxText,
                    normalizedToScreen(normalizedMaxValue) - maxTextWidth * 0.5f,
                    mDistanceToTop + mTextSize,
                    paint);
        }

    }

2 个答案:

答案 0 :(得分:0)

您可以创建一个MyRangeSeekBar类,它扩展RangeSeekBar并使用您的代码覆盖onDraw

答案 1 :(得分:0)

我找到了解决方案。下载&#34; android-range-seek-bar&#34;的源代码,然后用Android Studio打开它。现在您可以编辑代码了。完成编辑后,释放项目。得到&#34; .aar&#34;文件在&#34; rangeseekbar / build / outputs / aar&#34;夹。打开模块设置,单击添加模块,选择&#34;导入.JAR / .AAR包&#34;。导入刚刚生成的.aar文件。