OnTouch 更改背景图像
我正在努力在用户按下屏幕的四分之一时更改应用程序的背景图像。我已在下面发布了到目前为止我所做的事情,但不起作用。我也不知道如何引用我保存到资源文件夹中的 png 图片。谢谢。
public class TouchtwoActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
float x,y;
TextView tv = (TextView)findViewById(R.id.textv);
View vv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eight);
vv.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
if(x > getWindowManager().getDefaultDisplay().getWidth()/2)
{
changeImage();
}
return true;
}
public void changeImage()
{
getWindow().setBackgroundDrawable(Drawable.createFromPath("name of png from drawable));
}
}
I am working on changing the background image of my app when the user presses a quarter of the screen. I have posted what I have done so far below, but is not working. I am also not sure how to reference the png picture that I have saved into my resource folder. Thanks.
public class TouchtwoActivity extends Activity implements OnTouchListener {
/** Called when the activity is first created. */
float x,y;
TextView tv = (TextView)findViewById(R.id.textv);
View vv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.eight);
vv.setOnTouchListener(this);
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
x = event.getX();
y = event.getY();
if(x > getWindowManager().getDefaultDisplay().getWidth()/2)
{
changeImage();
}
return true;
}
public void changeImage()
{
getWindow().setBackgroundDrawable(Drawable.createFromPath("name of png from drawable));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先添加您创建的视图 (vv) 作为您在 XML 中创建的布局的子视图。然后使用布局参数设置视图 (vv) 的高度和宽度。
或者你可以在 XML 中创建一个视图并使用 findViewById 获取它,就像你对文本视图所做的那样。然后编写 vv.setOnTouchListener(this);
然后在 onTouch 中尝试
getWindow().setBackgroundDrawableResource (R.drawable.background_2);
First add the view (vv) u created as child of a layout u have created in the XML. Then set height and width to the view (vv) using layout params.
Or u can create a view in XML and get it using findViewById just like u did to the text view.Then write vv.setOnTouchListener(this);
Then in onTouch try
getWindow().setBackgroundDrawableResource(R.drawable.background_2);
试试这个
Try this