以下のように、バルーンの描画可能な形状を作成するにはどうすればよいですか。動的に色を変更できます。
75471 次
7 に答える
6
これを動的に保ちながら行うクリーンで正しい方法は、View クラスを拡張することです。
次に、onDraw で次のようにします。
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
drawBackground(canvas);
}
private void drawBackground(Canvas canvas) {
int width = (int) mWidth;
int height = (int) mHeight;
Point a = new Point(0, 0);
Point b = new Point(width, 0);
Point c = new Point(width, height - mPointHeight);//mPointedHeight is the length of the triangle... in this case we have it dynamic and can be changed.
Point d = new Point((width/2)+(mPointedHeight/2), height - mPointHeight);
Point e = new Point((width/2), height);// this is the sharp point of the triangle
Point f = new Point((width/2)-(mPointedHeight/2), height - mPointHeight);
Point g = new Point(0, height - mPointHeight);
Path path = new Path();
path.moveTo(a.x, a.y);
path.lineTo(b.x, b.y);
path.lineTo(c.x, c.y);
path.lineTo(d.x, d.y);
path.lineTo(e.x, e.y);
path.lineTo(f.x, f.y);
path.lineTo(g.x, g.y);
canvas.drawPath(path, mPointedBackgroundPaint);// mPointedBackgroundPaint is whatever color you want as the fill.
}
不要な階層化や、動的またはクリーンでないコードはありません。ボックスにテキストを追加することもできます。
于 2014-12-09T20:51:34.343 に答える
1
三角形の画像と長方形の画像を使用し、上記の形式で数学的に配置します。カラー フィルタリングを使用して、その色を動的に変更します。
ベクター グラフィックスやカスタム カラーを使用して、カスタム ビューに描画することもできます。これは、この問題を解決する別の方法です。
于 2014-02-26T13:21:09.397 に答える
1
カスタム ビューを作成し、canvas でトレイングルを描画する
package com.example.dickbutt;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
public class TriangleShapeView extends View {
public int colorCode = Color.MAGENTA;
public int getColorCode() {
return colorCode;
}
public void setColorCode(int colorCode) {
this.colorCode = colorCode;
}
public TriangleShapeView(Context context) {
super(context);
}
public TriangleShapeView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public TriangleShapeView(Context context, AttributeSet attrs) {
super(context, attrs);
}
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int w = getWidth() / 2;
int h = getHeight() / 2;
Path path = new Path();
path.moveTo(0, 0);
path.lineTo(w, 2 * h);
path.lineTo(2 * w, 0);
path.lineTo(0, 0);
path.close();
Paint p = new Paint();
p.setColor(colorCode);
p.setAntiAlias(true);
canvas.drawPath(path, p);
}
}
結果
使用法
<TextView
android:id="@+id/progress_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="@android:color/holo_purple"
android:gravity="center_horizontal"
android:text="200,0000000"
android:textColor="#fff" />
<com.example.dickbutt.TriangleShapeView
android:id="@+id/textView1"
android:layout_width="10dp"
android:layout_height="20dp"
android:layout_below="@+id/progress_value"
android:layout_centerHorizontal="true"
android:background="@drawable/rectangle"
android:gravity="center_horizontal"
android:textSize="10sp" />
利点
- ビューの幅と高さに応じて形状を変更します。
- 高度なカスタマイズが可能。
- よりきれいに見える
于 2016-07-14T09:58:15.397 に答える
0
まず、フォルダxml
内に1つ作成できますdrawable
それxml
は長方形の境界線の色を担当します
以下のコードでそのような境界線の形状を作成できます
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<solid android:color="#B2E3FA" />
</shape>
</item>
<item android:left="5dp" android:bottom="5dp" android:top="5dp" >
<shape android:shape="rectangle">
<solid android:color="#D8D8D8" />
</shape>
</item>
</layer-list>
これにより、必要な境界線が長方形に作成されます。このようなドローアブルでその長方形の背景を割り当てる必要があります
android:background="@drawable/bg"
bg
drawable フォルダーに保存されている xml ファイル名はどこですか
その後、その三角形を長方形オブジェクトの真下に配置する必要があります。
あなたが私の論理を理解してくれることを願っています
于 2014-02-26T13:23:55.260 に答える