how to set the seek bar thumb with a layout or with a TextView?

本秂侑毒 提交于 2019-12-21 20:47:09

问题


i want my seek bar to look like this...

the TextView should move with the thumb of seek bar... i tried with

seek_needle.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text=""
    />    
    <ImageView 
        android:layout_width="25dp"
        android:layout_height="fill_parent"
        android:src="@drawable/seek_needle"
        />

</LinearLayout>

and in mail.xml

  <SeekBar
                android:id="@+id/seekBar1"
                android:layout_width="349dp"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:max="202"
                android:layout_weight="80"
                android:thumb="@Layout/seek_needle"                  
                  >         </SeekBar>  

for this application is crashing
please help me out...

here is logcat output...

    09-11 17:50:23.582: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:23.762: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.062: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:24.212: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.582: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:24.632: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:24.664: D/dalvikvm(4142): GC_CONCURRENT freed 73K, 2% free 9435K/9607K, paused 7ms+8ms
09-11 17:50:24.862: D/AndroidRuntime(4142): Shutting down VM
09-11 17:50:24.872: W/dalvikvm(4142): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
09-11 17:50:24.952: E/AndroidRuntime(4142): FATAL EXCEPTION: main
09-11 17:50:24.952: E/AndroidRuntime(4142): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.Fm1Activity}: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
    09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.os.Looper.loop(Looper.java:137)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.main(ActivityThread.java:4424)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Method.invokeNative(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at dalvik.system.NativeStart.main(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.audioscreen/com.example.audioscreen.Fm1Activity}: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1797)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:682)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost.setCurrentTab(TabHost.java:346)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.TabHost.addTab(TabHost.java:236)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.example.audioscreen.MainActivity.onCreate(MainActivity.java:82)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.performCreate(Activity.java:4465)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 11 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: android.view.InflateException: Binary XML file line #63: Error inflating class android.widget.SeekBar
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:653)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:678)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:742)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.setContentView(Activity.java:1835)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at com.example.audioscreen.Fm1Activity.onCreate(Fm1Activity.java:24)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Activity.performCreate(Activity.java:4465)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 21 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: java.lang.reflect.InvocationTargetException
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Constructor.constructNative(Native Method)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
09-11 17:50:24.952: E/AndroidRuntime(4142):     ... 36 more
09-11 17:50:24.952: E/AndroidRuntime(4142): Caused by: android.content.res.Resources$NotFoundException: File res/layout/seek_needle.xml from drawable resource ID #0x7f030008
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.content.res.Resources.loadDrawable(Resources.java:1923)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
09-11 17:50:24.952: E/AndroidRuntime(4142):     at android.widget.AbsSeekBar.<init>(AbsSeekBar.java:70
09-11 17:50:25.032: D/dalvikvm(4142): GC_CONCURRENT freed 797K, 9% free 9183K/10055K, paused 7ms+7ms
09-11 17:50:25.203: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:25.232: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'
09-11 17:50:25.642: I/dalvikvm(4142): threadid=3: reacting to signal 3
09-11 17:50:25.662: I/dalvikvm(4142): Wrote stack traces to '/data/anr/traces.txt'

回答1:


This method takes a drawable from your resources, draws some text on top of it and returns the new drawable. All you need to do is give it the resource id of your bubble, and the text you want on top. Then you can pass the returned drawable wherever you want it.

public BitmapDrawable writeOnDrawable(int drawableId, String text){

    Bitmap bm = BitmapFactory.decodeResource(getResources(), drawableId).copy(Bitmap.Config.ARGB_8888, true);

    Paint paint = new Paint(); 
    paint.setStyle(Style.FILL);  
    paint.setColor(Color.BLACK); 
    paint.setTextSize(20); 

    Canvas canvas = new Canvas(bm);
    canvas.drawText(text, 0, bm.getHeight()/2, paint);

    return new BitmapDrawable(bm);
}

copied from.. Combine image and text to drawable

EDIT:

function call looks like this, mFreqbar.setThumb(writeOnDrawable(R.drawable.fm_radio_tune_position, mytext)); I have kept my fm_radio_tune_position.png file in res/drawable/ and 'mytext' is the string which i want to write on top of that drawable




回答2:


The function of the answer translated for who use MonoDroid / Xamarin.Android is the following:

public BitmapDrawable WriteOnDrawable(int drawableId, String text)
    {
        Bitmap bm = BitmapFactory.DecodeResource(Resources, drawableId).Copy(Bitmap.Config.Argb8888, true);

        Paint paint = new Paint();
        paint.SetStyle(Paint.Style.Fill);
        paint.Color = Color.Black;
        paint.TextSize = 20;

        Canvas canvas = new Canvas(bm);
        canvas.DrawText(text, 0, bm.Height / 2, paint);

        return new BitmapDrawable(bm);
    }


来源:https://stackoverflow.com/questions/12369519/how-to-set-the-seek-bar-thumb-with-a-layout-or-with-a-textview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!