我必须设计一个三角形并在其中显示一些角度为45的文本,低于我必须在三角形边界外放置文本视图以显示其他文本。它就像一面旗帜。但是,当我使用相对布局并放置三角形背景时,它仍然充当矩形,遮挡了我的文本视图。以下是我使用的代码:
<RelativeLayout
android:id="@+id/relative"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="@drawable/image_sticker" >
<com.example.AngledTextView
android:id="@+id/textViewx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:rotation="52"
android:textColor="#FFFFFF" />
</RelativeLayout>
我的AngledTextView类:
public class AngledTextView extends TextView {
private int mWidth;
private int mHeight;
public AngledTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
canvas.save();
/*Paint textPaint = new Paint();
int xPos = (canvas.getWidth() / 2);
int yPos = (int) ((canvas.getHeight() / 2) - ((textPaint.descent() + textPaint.ascent()) / 2)) ;
canvas.rotate(45, xPos,yPos); */
super.onDraw(canvas);
canvas.restore();
}
}
问题:
任何有关建议教程的提示或链接都将受到高度赞赏:)
答案 0 :(得分:6)
我最近做过类似的事情。以下是我使用的一些提示:
在onDraw方法上自定义路径。例如:
mPath = new Path();
mPath.moveTo(.0f, this.getHeight());
mPath.lineTo(this.getWidth(), this.getHeight());
mPath.lineTo(this.getWidth(),0.25f*this.getHeight());
mPath.lineTo(.0f, .0f);
mPath.lineTo(.0f, this.getHeight());
这将使路径类似于梯形。只需自定义您的点即可制作三角形。然后拨打
canvas.clipPath(mPath);
canvas.drawPath(mPath,mPaint);
通过这些点,您将绘制三角形。您可以将String传递给init方法并在绘制路径之前调用drawText:
canvas.drawText(str, xTit, yTit, mPaintTit);
我希望这会有所帮助=)
答案 1 :(得分:4)
在常规textview中使用旋转动画,如。
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:rotation="45"
android:text="@string/hello_world" />
答案 2 :(得分:4)
您的实施可以继承Drawable
并实施getTransparentRegion()
接下来,将此drawable用作在TextView
顶部绘制的视图的背景(可能同时将FrameView
中的两个作为子项)
答案 3 :(得分:3)
答案 4 :(得分:0)
在进行任何绘图之前,必须初始化并加载计划绘制的形状。除非程序中使用的形状的结构(原始坐标)在执行过程中发生变化,否则应在渲染器的onSurfaceCreated()方法中初始化它们,以获得内存和处理效率。
public void onSurfaceCreated(GL10 unused, EGLConfig config) {
...
// initialize a triangle
mTriangle = new Triangle();
// initialize a square
mSquare = new Square();
}
实施例
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Path;
import android.graphics.Point;
import android.util.AttributeSet;
import android.util.Log;
import android.widget.LinearLayout;
public class Slice extends LinearLayout {
final static String TAG = "Slice";
Paint mPaint;
Path mPath;
public enum Direction {
NORTH, SOUTH, EAST, WEST;
}
public Slice(Context context) {
super(context);
Create(context);
}
public Slice(Context context, AttributeSet attrs) {
super(context, attrs);
Create(context);
}
private void Create(Context context) {
Log.i(TAG, "Creating ...");
mPaint = new Paint();
mPaint.setStyle(Style.FILL);
mPaint.setColor(Color.RED);
Point point = new Point();
point.x = 80;
point.y = 80;
mPath = Calculate(point, 70, Direction.SOUTH);
}
@Override
protected void onDraw(Canvas canvas) {
Log.i(TAG, "Drawing ...");
canvas.drawPath(mPath, mPaint);
}
private Path Calculate(Point p1, int width, Direction direction) {
Log.i(TAG, "Calculating ...");
Point p2 = null, p3 = null;
if (direction == Direction.NORTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y - width);
} else if (direction == Direction.SOUTH) {
p2 = new Point(p1.x + width, p1.y);
p3 = new Point(p1.x + (width / 2), p1.y + width);
} else if (direction == Direction.EAST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x - width, p1.y + (width / 2));
} else if (direction == Direction.WEST) {
p2 = new Point(p1.x, p1.y + width);
p3 = new Point(p1.x + width, p1.y + (width / 2));
}
Path path = new Path();
path.moveTo(p1.x, p1.y);
path.lineTo(p2.x, p2.y);
path.lineTo(p3.x, p3.y);
return path;
}
}