我正在创建自定义弧形 View ,就像彩虹 View 一样。我可以绘制弧形 View ,但无法为每个 View 创建单独的点击事件。如何为每个弧形 View 设置单独的点击事件?。提前致谢。
代码如下:
ArcView.java
public class ArcView extends View implements View.OnTouchListener{
Paint paint;
int radius, x, y;
int color;
public ArcView(Context context) {
super(context);
init();
}
public ArcView(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ArcView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public ArcView(Context context, int radius, int x, int y, int color) {
super(context);
this.radius = radius;
this.x = x;
this.y = y;
this.color = color;
init();
}
private void init(){
paint = new Paint();
paint.setColor(color);
paint.setStrokeWidth(10);
paint.setStyle(Paint.Style.STROKE);
setOnTouchListener(this);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setStyle(Paint.Style.FILL);
canvas.drawCircle(x, y, radius, paint);
}
@Override
public boolean onTouch(View view, MotionEvent event) {
return false;
}
}
主 Activity .java
public class MainActivity extends AppCompatActivity {
RelativeLayout arcButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arcButton = (RelativeLayout) findViewById(R.id.arcButton);
arcButton1 = (RelativeLayout) findViewById(R.id.arcButton1);
arcButton2 = (RelativeLayout) findViewById(R.id.arcButton2);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int arcRadius1 = (int)(width/1.5);
int arcRadius2 = arcRadius1+(int)(width/3.5);
int arcRadius3 = arcRadius2+(int)(width/3.5);
int xCoor = width / 2;
int yCoor = height;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, height);
arcButton.setLayoutParams(params);
View arcView1=new ArcView(this, arcRadius3, xCoor, yCoor, Color.RED);
View arcView2=new ArcView(this, arcRadius2, xCoor, yCoor, Color.BLACK);
View arcView3=new ArcView(this, arcRadius1, xCoor, yCoor, Color.BLUE);
arcButton.addView(arcView1);
arcButton1.addView(arcView2);
arcButton2.addView(arcView3);
}
}
输出:
如何为每个弧形按钮创建单独的点击事件?
最佳答案
有点变通,但它有效。将其放入 ArcView 中:
@Override
public boolean performClick() {
return super.performClick();
}
@Override
public boolean onTouch(View view, MotionEvent event) {
view.performClick();
if (isPointInCircle((int) event.getX(), (int) event.getY())) {
// do what you want to do and get rid of the next two lines
String color = (((ArcView) view).color == Color.RED) ? "RED" : (((ArcView) view).color == Color.BLACK) ? "BLACK" : "BLUE";
Toast.makeText(context, color, Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
private boolean isPointInCircle(int clickX, int clickY) {
return (clickX - x) * (clickX - x) + (clickY - y) * (clickY - y) <= radius * radius;
}
请注意,要使 Toast 正常工作,您需要将 context 设为全局,但您很可能希望摆脱 Toast 无论如何。
关于java - 绘制自定义弧 View 并检测用户对每个弧的点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47237354/
我正在尝试设置一个puppet节点,但rubygems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由rubygems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby
我需要从一个View访问多个模型。以前,我的links_controller仅用于提供以不同方式排序的链接资源。现在我想包括一个部分(我假设)显示按分数排序的顶级用户(@users=User.all.sort_by(&:score))我知道我可以将此代码插入每个链接操作并从View访问它,但这似乎不是“ruby方式”,我将需要在不久的将来访问更多模型。这可能会变得很脏,是否有针对这种情况的任何技术?注意事项:我认为我的应用程序正朝着单一格式和动态页面内容的方向发展,本质上是一个典型的网络应用程序。我知道before_filter但考虑到我希望应用程序进入的方向,这似乎很麻烦。最终从任何
我想要做的是有2个不同的Controller,client和test_client。客户端Controller已经构建,我想创建一个test_clientController,我可以使用它来玩弄客户端的UI并根据需要进行调整。我主要是想绕过我在客户端中内置的验证及其对加载数据的管理Controller的依赖。所以我希望test_clientController加载示例数据集,然后呈现客户端Controller的索引View,以便我可以调整客户端UI。就是这样。我在test_clients索引方法中试过这个:classTestClientdefindexrender:template=>
我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer
我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢
我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby1.9+ 关于ruby-主要:Objectwhenrun
我将应用程序升级到Rails4,一切正常。我可以登录并转到我的编辑页面。也更新了观点。使用标准View时,用户会更新。但是当我添加例如字段:name时,它不会在表单中更新。使用devise3.1.1和gem'protected_attributes'我需要在设备或数据库上运行某种更新命令吗?我也搜索过这个地方,找到了许多不同的解决方案,但没有一个会更新我的用户字段。我没有添加任何自定义字段。 最佳答案 如果您想允许额外的参数,您可以在ApplicationController中使用beforefilter,因为Rails4将参数
我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/
我是一个Rails初学者,但我想从我的RailsView(html.haml文件)中查看Ruby变量的内容。我试图在ruby中打印出变量(认为它会在终端中出现),但没有得到任何结果。有什么建议吗?我知道Rails调试器,但更喜欢使用inspect来打印我的变量。 最佳答案 您可以在View中使用puts方法将信息输出到服务器控制台。您应该能够在View中的任何位置使用Haml执行以下操作:-puts@my_variable.inspect 关于ruby-on-rails-如何在我的R
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("