2012-01-12 68 views
8

Vì vậy, tôi đang cố gắng tự động thay đổi độ mờ của một TextView trong ứng dụng Android của tôi. Tôi có seekbar và khi tôi trượt ngón tay cái sang bên phải, TextView Tôi đã xếp lớp theo nó nên bắt đầu trở nên trong suốt. Khi ngón tay cái chạm đến một nửa trên seekbar, văn bản phải hoàn toàn trong suốt. Tôi đang cố gắng sử dụng phương pháp setAlpha(float) được kế thừa từ Chế độ xem trên TextView của tôi, nhưng Eclipse đang cho tôi biết setAlpha() là không xác định cho loại TextView. Tôi có gọi phương thức sai không? Hoặc là có một cách khác để thay đổi độ mờ đục?Thay đổi Độ mờ của TextView trong Android

Dưới đây là mã của tôi (classicTextTextView, gameSelectorseekbar):

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch){ 
    classicText.setAlpha(gameSelector.getProgress()); 
} 

Trả lời

37

bạn có thể đặt alpha như thế này

int alpha = 0; 
((TextView)findViewById(R.id.t1)).setTextColor(Color.argb(alpha, 255, 0, 0)); 

như alpha bạn nhận được từ seekbar sẽ được đặt vào màu sắc văn bản

5

phương pháp thay đổi sau

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromTouch) 
{ 
    classicText.setAlpha((float)(gameSelector.getProgress())/(float)(seekBar.getMax())); 
} 
+0

tính năng này không hoạt động AFAIK. tôi đã thử nó ..TextView không có phương thức có tên là setAlpha() .kiểm tra nó trước khi trả lời! – Hiral

+0

phương thức kiểm tra http://developer.android.com/reference/android/view/View.html#setAlpha(float) – jeet

+0

Đây là tài liệu tham khảo chính xác nhưng bạn sẽ không thể sử dụng phương pháp này để xem trực tiếp hoặc xem trực tiếp nhật thực của bạn .xin vui lòng kiểm tra yourself.instead, bạn cần phải tùy chỉnh textview và sau đó sử dụng lớp đó trong ứng dụng của bạn. – Hiral

-1

View.setAlpha (xxx float);

phạm vi xxx - 0 - 255, 0 trong suốt và 255 bị mờ.

int progress = gameSelector.getProgress(); 
int maxProgress = gameSelector.getMax(); 
float opacity = (progress/maxProgress)*255; 
classicText.setAlpha(opacity); 
9

này đã làm việc cho tôi:

1. Tạo lớp AlphaTextView.class:

public class AlphaTextView extends TextView { 

    public AlphaTextView(Context context) { 
    super(context); 
    } 

    public AlphaTextView(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    } 

    public AlphaTextView(Context context, AttributeSet attrs, int defStyle) { 
    super(context, attrs, defStyle); 
    } 

    @Override 
    public boolean onSetAlpha(int alpha) 
    { 
    setTextColor(getTextColors().withAlpha(alpha)); 
    setHintTextColor(getHintTextColors().withAlpha(alpha)); 
    setLinkTextColor(getLinkTextColors().withAlpha(alpha)); 
    getBackground().setAlpha(alpha); 
    return true; 
    }  
} 

2. Thêm này thay vì sử dụng TextView để tạo ra một TextView trong xml của bạn:

... 
    <!--use complete path to AlphaTextView in following tag--> 
    <com.xxx.xxx.xxx.AlphaTextView 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="sample alpha textview" 
     android:gravity="center" 
     android:id="@+id/at" 
     android:textColor="#FFFFFF" 
     android:background="#88FF88" 
     /> 
... 

3. Bây giờ bạn có thể sử dụng TextView này trong hoạt động của bạn như:

at=(AlphaTextView)findViewById(R.id.at); 

at.onSetAlpha(255); // To make textview 100% opaque 
at.onSetAlpha(0); //To make textview completely transperent 
+0

Tôi đã sử dụng một biến thể của điều này để thay đổi alpha văn bản mà không thay đổi alpha nền, cảm ơn! –

Các vấn đề liên quan