gpt4 book ai didi

android - 以编程方式绘制气泡

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:07:27 35 4
gpt4 key购买 nike

我想在我的应用程序中有一个带有百分比值的气泡,我不能使用 9 个补丁,因为我希望它是可定制的并且它的背景颜色应该是可变的。它应该看起来像这样 enter image description here

我该怎么做?这个气泡将在其中膨胀 View ,例如这个百分比或一些更大的布局。此外,根据布局(手机或平板电脑),它的一侧可能比另一侧大(箭头不在中心),所以这是我更喜欢以编程方式进行的另一个原因

最佳答案

创建一个自定义 Drawable 并将其用作您要将文本或其他 View 放入的任何容器的背景。
您将需要修改背景的填充以将气泡指针考虑在内。
下面的代码允许您将指针的对齐方式设置为左、中或右。
这只是给你一个想法的基本版本。您可以轻松地为气泡颜色添加一个 setter,或将描边属性添加到“mPaint”以获得额外的灵 active 。

public class BubbleDrawable extends Drawable {

// Public Class Constants
////////////////////////////////////////////////////////////

public static final int LEFT = 0;
public static final int CENTER = 1;
public static final int RIGHT = 2;

// Private Instance Variables
////////////////////////////////////////////////////////////

private Paint mPaint;
private int mColor;

private RectF mBoxRect;
private int mBoxWidth;
private int mBoxHeight;
private float mCornerRad;
private Rect mBoxPadding = new Rect();

private Path mPointer;
private int mPointerWidth;
private int mPointerHeight;
private int mPointerAlignment;

// Constructors
////////////////////////////////////////////////////////////

public BubbleDrawable(int pointerAlignment) {
setPointerAlignment(pointerAlignment);
initBubble();
}

// Setters
////////////////////////////////////////////////////////////

public void setPadding(int left, int top, int right, int bottom) {
mBoxPadding.left = left;
mBoxPadding.top = top;
mBoxPadding.right = right;
mBoxPadding.bottom = bottom;
}

public void setCornerRadius(float cornerRad) {
mCornerRad = cornerRad;
}

public void setPointerAlignment(int pointerAlignment) {
if (pointerAlignment < 0 || pointerAlignment > 3) {
Log.e("BubbleDrawable", "Invalid pointerAlignment argument");
} else {
mPointerAlignment = pointerAlignment;
}
}

public void setPointerWidth(int pointerWidth) {
mPointerWidth = pointerWidth;
}

public void setPointerHeight(int pointerHeight) {
mPointerHeight = pointerHeight;
}

// Private Methods
////////////////////////////////////////////////////////////

private void initBubble() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mColor = Color.RED;
mPaint.setColor(mColor);
mCornerRad = 0;
setPointerWidth(40);
setPointerHeight(40);
}

private void updatePointerPath() {
mPointer = new Path();
mPointer.setFillType(Path.FillType.EVEN_ODD);

// Set the starting point
mPointer.moveTo(pointerHorizontalStart(), mBoxHeight);

// Define the lines
mPointer.rLineTo(mPointerWidth, 0);
mPointer.rLineTo(-(mPointerWidth / 2), mPointerHeight);
mPointer.rLineTo(-(mPointerWidth / 2), -mPointerHeight);
mPointer.close();
}

private float pointerHorizontalStart() {
float x = 0;
switch (mPointerAlignment) {
case LEFT:
x = mCornerRad;
break;
case CENTER:
x = (mBoxWidth / 2) - (mPointerWidth / 2);
break;
case RIGHT:
x = mBoxWidth - mCornerRad - mPointerWidth;
}
return x;
}

// Superclass Override Methods
////////////////////////////////////////////////////////////

@Override
public void draw(Canvas canvas) {
mBoxRect = new RectF(0.0f, 0.0f, mBoxWidth, mBoxHeight);
canvas.drawRoundRect(mBoxRect, mCornerRad, mCornerRad, mPaint);
updatePointerPath();
canvas.drawPath(mPointer, mPaint);
}

@Override
public int getOpacity() {
return 255;
}

@Override
public void setAlpha(int alpha) {
// TODO Auto-generated method stub

}

@Override
public void setColorFilter(ColorFilter cf) {
// TODO Auto-generated method stub

}

@Override
public boolean getPadding(Rect padding) {
padding.set(mBoxPadding);

// Adjust the padding to include the height of the pointer
padding.bottom += mPointerHeight;
return true;
}

@Override
protected void onBoundsChange(Rect bounds) {
mBoxWidth = bounds.width();
mBoxHeight = getBounds().height() - mPointerHeight;
super.onBoundsChange(bounds);
}
}

用法
下面的示例展示了如何使用 BubbleDrawable。

MainActivity.java

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.myLayout);
BubbleDrawable myBubble = new BubbleDrawable(BubbleDrawable.CENTER);
myBubble.setCornerRadius(20);
myBubble.setPointerAlignment(BubbleDrawable.RIGHT);
myBubble.setPadding(25, 25, 25, 25);
linearLayout.setBackgroundDrawable(myBubble);
}
}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<LinearLayout
android:id="@+id/myLayout"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Text"
android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some Other Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>

</RelativeLayout>

关于android - 以编程方式绘制气泡,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20806673/

35 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com