根据按钮点击移动图像 [英] Moving the image according to the Button click
问题描述
我用四个按钮(上,下,左,右)和另外我有形象。我必须相应地移动图像时,我preSS UP按钮,我应该将图像向上的方向,当我preSS同样留下剩余的两个方向,应该将图像中左侧direction.I都的onclick侦听器,然后我用我试图利用移动X光图像,Y坐标ordinates.I怎么不拍X,Y坐标。
下面是code。
公共类MainActivity扩展活动实现OnClickListener {
按键向上,左,右,下,
ImageView的I1;
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
高达=(按钮)findViewById(R.id.button1);
左=(按钮)findViewById(R.id.button2);
右=(按钮)findViewById(R.id.button3);
下来=(按钮)findViewById(R.id.button4);
I1 =(ImageView的)findViewById(R.id.imageView1);
up.setOnClickListener(本);
}
公共无效的onClick(查看为arg0){
Toast.makeText(getApplication(),UP,5000).show();
RelativeLayout.LayoutParams mParams =(RelativeLayout.LayoutParams)
i1.getLayoutParams();
INT X =(int)的getRawx();
INT Y =(INT)getRawY();
mParams.leftMargin = X-50;
mParams.topMargin = Y-50;
i1.setLayoutParams(mParams);
}
}
您好, 我已经更新了以下code,请检查一下。
包com.example.motion;
公共类MainActivity扩展活动实现OnClickListener {
按键向上,左,右,下,
ImageView的I1;
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_main);
高达=(按钮)findViewById(R.id.button1);
左=(按钮)findViewById(R.id.button2);
右=(按钮)findViewById(R.id.button3);
下来=(按钮)findViewById(R.id.button4);
I1 =(ImageView的)findViewById(R.id.imageView1);
up.setOnClickListener(本);
down.setOnClickListener(本);
left.setOnClickListener(本);
right.setOnClickListener(本);
}
公共无效的onClick(视图v){
// TODO自动生成方法存根
开关(v.getId())
{
案例R.id.button1:
{
Toast.makeText(getApplication(),UP,Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams =(RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.topMargin - = 20;
i1.setLayoutParams(mParams);
打破;
}
案例R.id.button4:
{
Toast.makeText(getApplication(),向下,Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams =(RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.topMargin + = 20;
i1.setLayoutParams(mParams);
打破;
}
案例R.id.button2:
{
Toast.makeText(getApplication(),左,Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams =(RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.leftMargin - = 20;
i1.setLayoutParams(mParams);
打破;
}
案例R.id.button3:
{
Toast.makeText(getApplication(),右,Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams =(RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.leftMargin + = 20;
i1.setLayoutParams(mParams);
打破;
}
}
}
}
我认为这个问题是(INT)getRawx();
和( INT)getRawy();
,你正在呼吁这个活动的方法和我不太确定你真的需要这些值
您可以尝试:
mParams.leftMargin + = 50;
或
INT X = mParams.leftMargin;
mParams.leftMargin = X + 50
I used four buttons (Up,down,Left,Right) and also i have image. I have to move the image accordingly when i press UP button i should move image in upward direction and when i press left similarly remaining both direction it should move image in left direction.I have used onclick listener and then i am trying to move image using X,Y co-ordinates.I don't how take X,Y co-ordinates.
Here is the code.
public class MainActivity extends Activity implements OnClickListener {
Button up,left,right,down;
ImageView i1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up=(Button)findViewById(R.id.button1);
left=(Button)findViewById(R.id.button2);
right=(Button)findViewById(R.id.button3);
down=(Button)findViewById(R.id.button4);
i1=(ImageView)findViewById(R.id.imageView1);
up.setOnClickListener(this);
}
public void onClick(View arg0) {
Toast.makeText(getApplication(),"UP",5000).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
int x = (int)getRawx();
int y = (int)getRawY();
mParams.leftMargin = x-50;
mParams.topMargin = y-50;
i1.setLayoutParams(mParams);
}
}
HI, I have updated code below please check it.
package com.example.motion;
public class MainActivity extends Activity implements OnClickListener {
Button up,left,right,down;
ImageView i1;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
up=(Button)findViewById(R.id.button1);
left=(Button)findViewById(R.id.button2);
right=(Button)findViewById(R.id.button3);
down=(Button)findViewById(R.id.button4);
i1=(ImageView)findViewById(R.id.imageView1);
up.setOnClickListener(this);
down.setOnClickListener(this);
left.setOnClickListener(this);
right.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.button1:
{
Toast.makeText(getApplication(),"UP",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.topMargin -= 20;
i1.setLayoutParams(mParams);
break;
}
case R.id.button4:
{
Toast.makeText(getApplication(),"DOWN",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.topMargin += 20;
i1.setLayoutParams(mParams);
break;
}
case R.id.button2:
{
Toast.makeText(getApplication(),"LEFT",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.leftMargin -= 20;
i1.setLayoutParams(mParams);
break;
}
case R.id.button3:
{
Toast.makeText(getApplication(),"RIGHT",Toast.LENGTH_SHORT).show();
RelativeLayout.LayoutParams mParams = (RelativeLayout.LayoutParams)
i1.getLayoutParams();
mParams.leftMargin += 20;
i1.setLayoutParams(mParams);
break;
}
}
}
}
I think the problem is (int)getRawx();
and (int)getRawy();
, you are calling this methods on Activity and I'm not so sure you really need those values.
you could try:
mParams.leftMargin += 50;
or
int x = mParams.leftMargin;
mParams.leftMargin = x + 50
这篇关于根据按钮点击移动图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!