在类中绘制一条延伸“活动”的行 [英] Drawing a line in a class which extends "Activity'
问题描述
我有一个ImageView在我的Activity中加载一张图片,并希望在其上绘制一些线条。好像我无法将我绘制的线条叠加到ImageView上。如果我创建一个扩展'View'的新类(myPainter),从活动中挂钩myPainter.onDraw()然后在onCreate()中调用setContentView(mp),我得到的就是绘制的线条。我的ImageView已从onCreate()中删除。如何让ImageView和线条以相同的布局显示?我也为ImageView覆盖了onTouch(),所以我可以从中获取xy点击位置。
I have an ImageView loading a picture in my Activity and want to draw some lines on it. It seems like I'm not able to overlay the lines I draw onto the ImageView. If I create a new class (myPainter) which extends 'View', hook myPainter.onDraw() and then setContentView(mp) in onCreate() from the activity, all I get are the drawn lines. My ImageView is gone from onCreate(). How do I get the ImageView and lines to show up in the same layout? I'm also Override'ing onTouch() for the ImageView so I can get the xy click locations from it.
public class myPainter extends View
{
public myPainter (Context context)
{
super (context);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw (canvas);
Paint p = new Paint();
p.setColor(Color.RED);
canvas.drawLine (E_XMIN, E_YMIN, E_XMAX, E_YMAX, p);
}
}
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
ImageView iv = (ImageView) findViewById(R.id.ImageView01);
iv.setOnTouchListener (this);
// this draws my lines, but I loose the pic above ^^^
myPainter mp = new myPainter(this);
setContentView(mp);
}
更新:
我想我已经把自己画成了一个角落。我现在如何在onTouchEvent()方法中向此代码添加frameAnimation?就像用户触摸屏幕的地方一样。如果我将代码添加到myPainter()类,Android rocketAnimation示例会导致异常:
I think I've literally painted myself into a corner. How would I add a frameAnimation to this code now in the onTouchEvent() method? Like where the user touches the screen. The Android rocketAnimation example causes an exception if I add code to the myPainter() class:
@Override
public boolean onTouchEvent (MotionEvent event)
{
//Log.d (TAG, "touchevent in myPainter");
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d (TAG, "down x:" + event.getX() + " y:" + event.getY());
int wClick = getClick (event.getX(), event.getY());
AnimationDrawable touchAni;
ImageView iv = (ImageView) findViewById (R.drawable.triangle);
iv.setBackgroundResource (R.drawable.nova);
touchAni = (AnimationDrawable) iv.getBackground ();
touchAni.start ();
return true;
}
return super.onTouchEvent (event);
}
推荐答案
我试图实施建议以上,但无法获得我的目标功能。也许我没有完全解决我的问题,或者只是一般性的误解。我最终在myPainter main方法中添加了一个setBackgroundResource()来获取屏幕上的图像。我想我已经按需要工作并发布下面的代码,以防它帮助其他人。感谢您提供帮助!
I tried to implement the suggestions above, but could not get the functionality I was aiming for. Perhaps I've not framed my question completely, or just a general misunderstanding. I ended up adding a setBackgroundResource() in the myPainter main method to get the image on the screen. I think I've got it working as needed and posting the code below in case it helps someone else. Thanks for offering your help!
class myPainter extends View implements myConst
{
private final static String TAG = "** myPainter **";
public myPainter (Context context)
{
super (context);
Log.d (TAG, "new myPainter");
setBackgroundResource (R.drawable.triangle);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw (canvas);
Log.d (TAG, "new myPainter");
Paint p = new Paint ();
p.setColor (Color.RED);
canvas.drawLine (E_XMIN, E_YMIN, E_XMAX, E_YMIN, p);
}
/**
* called to determine what part of the triangle was clicked
* @param x
* @param y
* @return click id from myConst
*/
private int getClick (float x, float y)
{
int id = 0;
// insert logic to test x,y locations and return
// region ID of what was clicked
...
return (id);
}
@Override
public boolean onTouchEvent (MotionEvent event)
{
Log.d (TAG, "touchevent in myPainter");
switch (event.getAction())
{
case MotionEvent.ACTION_DOWN:
Log.d (TAG, "down x:" + event.getX() + " y:" + event.getY());
int wClick = getClick (event.getX(), event.getY());
break;
}
return false;
}
}
然后我的主应用程序类有这对于onCreate():
And then my main application class has this for onCreate():
/** Called when the activity is first created. */
@Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
myPainter mp = new myPainter(getApplicationContext());
mp.invalidate ();
setContentView(mp);
//ImageView iv = (ImageView) findViewById(R.id.ImageView01);
//iv.setOnTouchListener (this);
}
这篇关于在类中绘制一条延伸“活动”的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!