OnTouch 更改背景图像

发布于 2024-12-31 21:38:20 字数 925 浏览 5 评论 0原文

我正在努力在用户按下屏幕的四分之一时更改应用程序的背景图像。我已在下面发布了到目前为止我所做的事情,但不起作用。我也不知道如何引用我保存到资源文件夹中的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

夜雨飘雪 2025-01-07 21:38:20

首先添加您创建的视图 (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);

回忆凄美了谁 2025-01-07 21:38:20

试试这个

getWindow().setBackgroundDrawableResource(R.drawable.background_2);

Try this

getWindow().setBackgroundDrawableResource(R.drawable.background_2);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文