- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
到目前为止,我能够使用一些代码引用表单 stackoverflow 绘制半圆。现在我想在这个半圆上画针。我不知道该怎么做。这是半圆代码
package in.ashish29agre.stackoverflow.sample.semicircle;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.RectF;
import android.view.View;
public class MyGraphView extends View {
private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
private float[] value_degree;
private int[] COLORS = { Color.GREEN, Color.RED, Color.GREEN, Color.RED };
// size of bigger half circle
RectF rectf = null;
// size of smaller half circle
// RectF rectf2 = new RectF(45, 45, 275, 275);
// size of the smallest half circle
// RectF rectf3 = new RectF(80, 80, 240, 240);
int temp = 0;
public MyGraphView(Context context, float[] values) {
super(context);
rectf = new RectF();
value_degree = new float[values.length];
for (int i = 0; i < values.length; i++) {
value_degree[i] = values[i];
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int width = getMeasuredWidth();
int height = getMeasuredHeight();
rectf.set(100, 100, width - 100, height - 100);
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
for (int i = 0; i < value_degree.length; i++) {
// set type of "brush"
paint.setStrokeWidth(128);
paint.setStyle(Paint.Style.STROKE);
// agree
if (i == 0) {
final Path path = new Path();
paint.setColor(COLORS[i]);
// draw 3 paths to show 3 curves
path.addArc(rectf, 180, value_degree[i] - 4);
// path.addArc(rectf2, 180, value_degree[i] - 5);
// path.addArc(rectf3, 180, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
// disagree
} else {
temp += (int) value_degree[i - 1];
paint.setColor(COLORS[i]);
final Path path = new Path();
path.addArc(rectf, temp + 180 , value_degree[i] - 0);
// path.addArc(rectf2, temp + 180 + 5, value_degree[i] - 5);
// path.addArc(rectf3, temp + 180 + 6, value_degree[i] - 6);
// draw the path
canvas.drawPath(path, paint);
}
drawLine(canvas);
}
}
private void drawLine(Canvas canvas) {
float startAngle = 180;
float sweepAngle = 180;
Paint mPaint = new Paint();
mPaint.setStrokeWidth(22f);
mPaint.setColor(Color.BLACK);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
float startX = rectf.centerX();
float startY = rectf.centerY();
float radius = rectf.centerX() - 100;
float angle = (float) ((startAngle + sweepAngle / 2) * Math.PI / 180);
float stopX = (float) (startX + radius * Math.cos(angle));
float stopY = (float) (startY + radius * Math.sin(angle));
canvas.drawLine(startX, startY, stopX, stopY, mPaint);
}
}
从 Activity 我调用它使用
float values[] = { 10, 20, 30, 40};
LinearLayout linear = (LinearLayout) findViewById(R.id.graph_view);
values = calculateData(values);
// draw the graf
linear.addView(new MyGraphView(this, values));
这是我的 Activity xml 代码
<?xml version="1.0" encoding="utf-8"?>
<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="in.ashish29agre.stackoverflow.sample.semicircle.SemiCircleActivity">
<LinearLayout
android:id="@+id/graph_view"
android:layout_width="200dp"
android:layout_height="200dp"
android:orientation="vertical"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"></LinearLayout>
</RelativeLayout>
目前 View 看起来像这样现在我想添加可以从 0-180 度移动的仪表指针。
我们将不胜感激。
现在我已经画好了针,我想制作一个针位图??
最佳答案
您可能想要为针位图设置动画。这是我的代码:
RotateAnimation animation =
new RotateAnimation(
mCurrentAngle,
toAngle,
Animation.RELATIVE_TO_SELF,
pivotX,
Animation.RELATIVE_TO_SELF,
pivotY);
animation.setRepeatCount(0);
animation.setFillAfter(true);
animation.setFillEnabled(true);
animation.setInterpolator(new OvershootInterpolator());
animation.setDuration(duration);
mMeterPointer.startAnimation(animation);
其中 mMeterPointer 是指针的 ImageView。
pivotX
和 pivotY
值应位于针图像应旋转的位置。它们的值应该是 0 到 1
,例如0.5 和 0.5 如果你想让它在位图的中间旋转。
关于android - 编辑 : Android Draw needle image to this circle similar to meter guage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35102161/
我的第一个堆栈溢出问题,所以我希望它格式正确。 我制作了一个模拟 strstr 的简单函数: char *my_strstr(char *haystack, char *needle) { in
假设我正在尝试在任何领域搜索“Douglas”。如果我这样做: solr/query?q=Douglas 我收到 0 条回复,但如果我这样做: solr/query?q=firstname:Dougl
我有一个字符串,看起来有点像这样 1: u:mads g:folk 2: g:andre u:jens u:joren 我需要的是一种获取例如 u:jens 和它后面的数字(1 或 2)的方法(我猜测
我通过引用以下链接中的代码在我的项目中实现了速度计。 我需要对速度计指针进行动画处理,直到从服务器获得结果,一旦获得结果,我需要根据一些计算将指针设置为正确的值。 我不明白该怎么做。 请帮我解决问题。
我写了一个小方法来查看一个字符串是否包含另一个字符串。不过我只有一个小问题,它总是返回 false。 给定大海捞针是一个名为 salaryCheck 的字符串,其值为“10.1”,针是“.”。它总是返
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
有一个内置函数用于查找值的数组键 - array_search .然而,正如您从示例中看到的那样,该函数仅查找第一次出现的情况,而我需要最后一次出现: 'blue', 1 => 'red', 2 =
假设这可以正确判断子字符串是否在字符串中,是否有更简洁的方法来做到这一点? if(is_int(strpos($haystack, $needle))){ ... } 最佳答案 我不会那样做的。
我从以下代码中得到了一个奇怪的行为..它的目的是在每次按键时为用户提供自动完成选项 function autoComplete(str) { debugger; if (str
是否有一个函数可以检查数组中的某些值?我有 2 个数组,1 个带有文件扩展名,1 个带有混合内容。 In_array 仅适用于字符串。 谢谢了! 文件示例: picture21474.png 允许的扩
是否有检查数组中某些值的函数?我有 2 个数组,1 个带有文件扩展名,1 个带有混合内容。 In_array 仅处理字符串。 已经谢谢了! 文件示例: picture21474.png 允许的扩展名:
我需要一个针盘,显示实时值。该值改变针的角度,而不是位置。 到目前为止,我所能做的就是使用 ray() 或 annular_wedge() 字形,但是这个解决方案有很多问题:前面的行不是删除,几次更新
我有一张图片。在该图像上,我放置了一个针图像,我想将其旋转一定 Angular 。一切都运行良好。我想要的是改变针的 0 度位置。目前它在顶部,我想在底部制作 0 度点。请参阅下面的附图。 这是我为此
我想在流模式下使用 node.js 的 needle 模块,类似于 needle 文档中的这个例子: var stream = needle.get('http://www.as35662.net/1
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 9 年前。 Improve th
我有以下代码,它从命令行获取参数并大海捞针。 #include #include int main(int argc, char **argv) { int i, j, flag = 0;
我想将一个小图像从一台服务器移动到另一台服务器(都运行 Node )。当我搜索时,我还没有找到足够的东西。 This该帖子仍未得到答复。 当我开始试验时,我向第一台服务器写入了以下内容: app.po
请求模块有参数localAddress。 options = { url: "https://ru.tradeskinsfast.com/ajax/botsinventory",
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
下面的代码采用一个关键字和一串文本(经过 html 标记清理),并确定该关键字是否出现在经过清理的内容的最后一句话中。 有一个小故障我想不通。当内容末尾包含一个空格或带有不间断空格的段落标签时,即 T
我是一名优秀的程序员,十分优秀!