2010-04-01 37 views
27

Làm cách nào để hiển thị văn bản lộn ngược với chế độ xem văn bản trong Android?Làm cách nào để hiển thị văn bản lộn ngược với chế độ xem văn bản trong Android?

Trong trường hợp của tôi, tôi có trò chơi 2 người chơi, nơi họ chơi với nhau. Tôi muốn hiển thị bài kiểm tra cho người chơi thứ hai được định hướng cho họ.


Đây là giải pháp tôi thực hiện sau khi AaronMs lời khuyên

Lớp mà không được trọng, bab.foo.UpsideDownText

package bab.foo; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.util.AttributeSet; 
import android.widget.TextView; 

public class UpsideDownText extends TextView { 

    //The below two constructors appear to be required 
    public UpsideDownText(Context context) { 
     super(context); 
    } 

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

    @Override 
    public void onDraw(Canvas canvas) { 
     //This saves off the matrix that the canvas applies to draws, so it can be restored later. 
     canvas.save(); 

     //now we change the matrix 
     //We need to rotate around the center of our text 
     //Otherwise it rotates around the origin, and that's bad. 
     float py = this.getHeight()/2.0f; 
     float px = this.getWidth()/2.0f; 
     canvas.rotate(180, px, py); 

     //draw the text with the matrix applied. 
     super.onDraw(canvas); 

     //restore the old matrix. 
     canvas.restore(); 
    } 
} 

Và đây là cách bố trí XML của tôi:

<bab.foo.UpsideDownText 
    android:text="Score: 0" 
    android:id="@+id/tvScore" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textColor="#FFFFFF" 
    > 
</bab.foo.UpsideDownText> 

Trả lời

27

trong file xml add:

android:rotation = "180" 

trong các yếu tố tương ứng để hiển thị văn bản lộn ngược.

ví dụ:

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:gravity="center" 
     android:text="TextView" 
     android:rotation="180"/> 
+0

Hrm Khi tôi hỏi câu hỏi này không có sẵn. Tôi đã trên 1,6 bạn có biết nếu điều này đã được thêm vào một bản phát hành sau này? – Ravedave

+0

Chỉ có sẵn trong API 11+ – AndroidDev

+0

Hãy dùng thử Ứng dụng của tôi bị lỗi, –

5

Tôi chưa thử làm việc này, nhưng tôi nghĩ nó sẽ hoạt động.

Ghi đè phương thức onDraw của chế độ xem, sau khi gọi phương thức xoay trên canvas, sau khi gọi phương thức xoay trên canvas được chuyển đến canvas, chuyển 180 hoặc Math.PI, tùy thuộc vào việc nó hoạt động ở độ hoặc radian.

+0

Bất cứ ai có ý tưởng tại sao điều đó không hoạt động? – Ravedave

+4

Bạn chỉ có thể chia tỷ lệ -1 trên trục Y quá :) –

+0

Cảm ơn tôi đã xóa nhận xét của tôi yêu cầu trợ giúp thêm vì mã đó không hoạt động :). Cảm ơn câu trả lời nó chỉ cho tôi đi đúng hướng. – Ravedave

3

mã bên dưới hoạt động hoàn hảo (cảm ơn bằng cách này). cố gắng thêm hàm khởi tạo bị thiếu. nếu nó không được làm việc các vấn đề của tôi là android phiên bản chính là 2,1

package p.c; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.util.AttributeSet; 
import android.util.Log; 
import android.widget.TextView; 

public class TextViewUD extends TextView { 

    public TextViewUD(Context context) { 
     super(context); 
     // TODO Auto-generated constructor stub 
    } 

    public TextViewUD(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     // TODO Auto-generated constructor stub 
    } 

    public TextViewUD(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     // TODO Auto-generated constructor stub 
    } 

    @Override 
    protected void onDraw(Canvas canvas) { 
     canvas.save(); 

     float py = this.getHeight()/2.0f; 
     float px = this.getWidth()/2.0f; 
     Log.d("testUD", String.format("w: %d h: %d ", this.getWidth(), this.getHeight())); 
     Log.d("testUD", String.format("w: %f h: %f ", py, px)); 
     canvas.rotate(180, px, py); 

     super.onDraw(canvas); 

     canvas.restore(); 
    } 
} 
+0

Hoàn hảo! Hoạt động tốt cho các phiên bản Android cũ hơn! – Nick

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