实施OnTouchListener上的LinearLayout - Android的发展 [英] Implementing OnTouchListener on LinearLayout - Android Development
问题描述
public class UnitConverterActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
LinearLayout mLinearLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mLinearLayout = new LinearLayout(this);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.mainmenu);
//i.setAdjustViewBounds(false);
i.setScaleType(ScaleType.FIT_XY);
i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
mLinearLayout.addView(i);
setContentView(mLinearLayout);
//setContentView(R.layout.main);
}
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return false;
}
}
我用上面的方法来加载图像的主菜单我试图创造。图像具有四个区域,每个将被用于调用该应用的特定功能。现在我想实现在这些领域的触摸界面。我知道如何定义用于这一目的的像素范围,但我在就如何落实OnTouchListner在图像上的损失。请帮我在这方面。
I have used the above method to load an image for the main menu I am trying to create. The image has four areas and each will be used to call a particular function of the app. Now I am trying to implement touch interface on those areas. I know how to define the range of pixels for that purpose but I am at loss on how to implement OnTouchListner on the image. Please help me in this regard.
推荐答案
如果你的形象被分成四个长方形季度(说)
If your image was split into four rectangular quarters (say)
再有的onCreate:
then in onCreate have:
i.setOnTouchListener(this);
和你的听众,像这样(说明唯一原则):
and for your listener, something like this (illustrates the principle only):
@Override
public boolean onTouch(View v, MotionEvent mev) {
int width = v.getWidth();
int height = v.getHeight();
float x = mev.getX();
float y = mev.getY();
String msg;
if (x < width / 2) {
if (y < height / 2)
msg = "Top left quarter";
else
msg = "Bottom left quarter";
} else {
if (y < height / 2)
msg = "Top right quarter";
else
msg = "Bottom right quarter";
}
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
return false;
}
这篇关于实施OnTouchListener上的LinearLayout - Android的发展的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!