Привет, я сделал небольшую фигуру, и мои утверждения if начинают выходить из-под контроля, поэтому я сделал более простую тестовую сценарную игру, чтобы попытаться упростить код, но я не так опытен с java и андроид как id, как быть, поэтому, попробовав несколько вещей, я думал, что id задает вопрос, как мне сделать этот код меньше? В настоящее время я использую onTouchListeners и onDragListeners для нескольких форм, 3 цветных фигур и 3 пустых 'пустых фигуры', и когда они связаны, помещая один поверх другого, пустая форма становится цветной ... очень простой. но берет тонну кода Вот что я имелкак сделать мой код короче
@Override
public boolean onTouch(View v, MotionEvent e) {
if (e.getAction() == MotionEvent.ACTION_DOWN) {
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(v);
v.startDrag(null, shadowBuilder, v, 0);
sp.play(dragSound, 1, 1, 0, 0, 1);
return true;
} else {
return false;
}
}
//WHEN DRAGGED AND DROPPED
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction()==DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
//IF THEY MATCH
if(view.getId()==R.id.squareImage && v.getId()==R.id.squareImage1)
{
ViewGroup from = (ViewGroup) view.getParent();
ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
View congrats = findViewById(R.id.top_layout);
ViewGroup two = (ViewGroup) findViewById(R.id.layout3);
from.removeView(view);
v.setBackgroundResource(R.drawable.dragsquare);
sp.play(dropSound, 1, 1, 0, 0, 1);
sb.setVisibility(View.VISIBLE);
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setVisibility(View.VISIBLE);
if (to.getChildCount()< 1 && two.getChildCount()< 1)
{
congrats.setVisibility(View.VISIBLE);
imageView.setBackgroundResource(R.drawable.congrats);
sun=(AnimationDrawable)imageView.getBackground();
sun.start();
sp.play(tada, 1, 1, 0, 0, 1);
congrats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View congrats
(RelativeLayout)findViewById(R.id.top_layout);
congrats.setVisibility(View.INVISIBLE);
}
});
}
//2 square balloons floating
sb = (ImageView)findViewById(R.id.squareballoon);
sb.setVisibility(View.VISIBLE);
sb2 = (ImageView)findViewById(R.id.squareballoon2);
sb2.setVisibility(View.VISIBLE);
sp.play(inflate, 1, 1, 0, 0, 1);
ObjectAnimator sqbalAnim3=
ObjectAnimator.ofFloat(sb2,"x",-500,500);
sqbalAnim3.setDuration(700);
sqbalAnim3.setRepeatCount(20);
sqbalAnim3.setRepeatMode(ValueAnimator.REVERSE);
ObjectAnimator sqbalAnim =
ObjectAnimator.ofFloat(sb2,"y",1800,-1800);
sqbalAnim.setDuration(3000);
sqbalAnim.setRepeatMode(ValueAnimator.RESTART);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
sb2.setVisibility(View.GONE);
}
});
animSetXY.playTogether(sqbalAnim, sqbalAnim3);
animSetXY.setStartDelay(20);
animSetXY.start();
ObjectAnimator sqbal2Anim =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"y",-450);
sqbal2Anim.setDuration(3000);
sqbal2Anim.setRepeatMode(ValueAnimator.RESTART);
ObjectAnimator sqbalAnim4 =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"x",650,750);
sqbalAnim4.setStartDelay(20);
sqbalAnim4.setDuration(300);
sqbalAnim4.setRepeatCount(6);
sqbalAnim4.setRepeatMode(ValueAnimator.REVERSE);
AnimatorSet animSetXY2 = new AnimatorSet();
animSetXY2.playTogether(sqbal2Anim,sqbalAnim4);
animSetXY2.start();
return true;}
//end of square balloons
else if(view.getId()==R.id.circleImage &&
v.getId()==R.id.circleImage1){
ViewGroup from = (ViewGroup) view.getParent();
ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
View congrats = findViewById(R.id.top_layout);
from.removeView(view);
v.setBackgroundResource(R.drawable.dragcircle);
sp.play(dropSound, 1, 1, 0, 0, 1);
cb = (ImageView)findViewById(R.id.circleballoon);
cb.setVisibility(View.VISIBLE);
imageView.setVisibility(View.VISIBLE);
ViewGroup two = (ViewGroup) findViewById(R.id.layout3);
if (to.getChildCount()< 1 && two.getChildCount()< 1)
{
congrats.setVisibility(View.VISIBLE);
imageView.setBackgroundResource(R.drawable.congrats);
sun=(AnimationDrawable)imageView.getBackground();
sun.start();
sp.play(tada, 1, 1, 0, 0, 1);
congrats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View congrats
(RelativeLayout)findViewById(R.id.top_layout);
congrats.setVisibility(View.INVISIBLE);
}
});
}
//circle balloons floating
cb = (ImageView)findViewById(R.id.circleballoon);
cb.setVisibility(View.VISIBLE);
cb2 = (ImageView)findViewById(R.id.circleballoon2);
cb2.setVisibility(View.VISIBLE);
sp.play(inflate, 1, 1, 0, 0, 1);
ObjectAnimator sqbalAnim3 =
ObjectAnimator.ofFloat(cb,"x",-500,500);
sqbalAnim3.setDuration(700);
sqbalAnim3.setRepeatCount(20);
sqbalAnim3.setRepeatMode(ValueAnimator.REVERSE);
ObjectAnimator sqbalAnim =
ObjectAnimator.ofFloat(cb,"y",1800,-1800);
sqbalAnim.setDuration(3000);
sqbalAnim.setRepeatMode(ValueAnimator.RESTART);
AnimatorSet animSetXY = new AnimatorSet();
animSetXY.addListener(new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
super.onAnimationEnd(animation);
cb.setVisibility(View.GONE);
}
});
animSetXY.playTogether(sqbalAnim, sqbalAnim3);
animSetXY.setStartDelay(20);
animSetXY.start();
ObjectAnimator sqbal2Anim =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"y",-450);
sqbal2Anim.setDuration(3000);
sqbal2Anim.setRepeatMode(ValueAnimator.RESTART);
ObjectAnimator sqbalAnim4 =
ObjectAnimator.ofFloat(findViewById(R.id.squareballoon2),"x",650,750);
sqbalAnim4.setStartDelay(20);
sqbalAnim4.setDuration(300);
sqbalAnim4.setRepeatCount(6);
sqbalAnim4.setRepeatMode(ValueAnimator.REVERSE);
AnimatorSet animSetXY2 = new AnimatorSet();
animSetXY2.playTogether(sqbal2Anim,sqbalAnim4);
animSetXY2.start();
return true;}
} else if(view.getId()==R.id.triangleImage &&
v.getId()==R.id.triangleImage1){
ViewGroup from = (ViewGroup) view.getParent();
ViewGroup to = (ViewGroup) findViewById(R.id.layout2);
View congrats = findViewById(R.id.top_layout);
from.removeView(view);
v.setBackgroundResource(R.drawable.dragtriangle);
sp.play(dropSound, 1, 1, 0, 0, 1);
tb = (ImageView)findViewById(R.id.triballoon);
tb.setVisibility(View.VISIBLE);
imageView.setVisibility(View.VISIBLE);
ViewGroup two = (ViewGroup) findViewById(R.id.layout3);
if (to.getChildCount()< 1 && two.getChildCount()< 1)
{
congrats.setVisibility(View.VISIBLE);
imageView.setBackgroundResource(R.drawable.congrats);
sun=(AnimationDrawable)imageView.getBackground();
sun.start();
sp.play(tada, 1, 1, 0, 0, 1);
congrats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
View congrats = findViewById(R.id.top_layout);
congrats.setVisibility(View.INVISIBLE);
}
});
}
AnimatorSet sunSet = (AnimatorSet)
AnimatorInflater.loadAnimator(this, R.animator.float1);
sunSet.setTarget(tb);
sunSet.start();
tb = (ImageView)findViewById(R.id.triballoon);
AnimatorSet sunnySet = (AnimatorSet)
AnimatorInflater.loadAnimator(this, R.animator.float2);
sunnySet.setTarget(tb);
sunnySet.start();
ImageView tb2 = (ImageView)findViewById(R.id.triballoon2);
tb2.setVisibility(View.VISIBLE);
AnimatorSet sunSet1 = (AnimatorSet)
AnimatorInflater.loadAnimator(this, R.animator.float3);
sunSet1.setTarget(tb2);
sunSet1.start();
tb2 = (ImageView)findViewById(R.id.triballoon2);
tb2.setVisibility(View.VISIBLE);
AnimatorSet sunnySet1 = (AnimatorSet)
AnimatorInflater.loadAnimator(this, R.animator.float2);
sunnySet1.setTarget(tb);
sunnySet1.start();
sp.play(inflate, 1, 1, 0, 0, 1);
return true;
Это был мой оригинал, что я хочу знать, есть ли способ, чтобы использовать оператор OR, чтобы поместить их все в том же заявлении, но по-прежнему дают разные результаты для каждой формы что-то вроде этого
@Override
public boolean onDrag(View v, DragEvent e) {
if (e.getAction() == DragEvent.ACTION_DROP) {
View view = (View) e.getLocalState();
//IF THEY MATCH
if (view.getId() == R.id.squareshape && v.getId() ==
R.id.emptysquare || view.getId() == R.id.circleshape && v.getId() ==
R.id.emptycircle|| view.getId() == R.id.trishape && v.getId() ==
R.id.emptytri) {
//view.getId().(v.getId()); view.setBackgroundResource(v)
mt_sq.setImageResource(R.drawable.dragsquare);
}else{
//do nothing
}
}
return true;
}
комментарий в середине является то, что я хочу, чтобы достичь //view.getId() (v.getId()). view.setBackgroundResource (v), но очевидно, что это дает мне ошибки, может ли кто-нибудь предложить решение или мне просто нужно продолжать грузоперевозки по моему оригиналу? любые предложения приветствуются
Используйте библиотеку ButterKnife от Jake Wharton, поэтому вы не используете findViewById постоянно. –
Butter knife выглядит великолепно, я читал через сайт, но я не знал бы, как это реализовать, но для более опытных кодеров это будет ответ, поэтому я буду отмечать это как ответ скоро, если кто-то не весит с более простым решением. вы @Kevin Crain – martinseal1987
Его не для опытных программистов, его легко реализовать и использовать. :) –