마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

커스텀 뷰를 xml에서 쓰고싶어요!!

0 추천

 

이런식으로  커스텀뷰를 만들었는데

 

그안에 이미지뷰를 한개 넣으니 에러가 뜨네요 ㅠㅠㅠ

 

 

커스텀뷰 자체만 xml에서 해놓으면 문제가 없는데말이죠 ㅠ

 

 

커스텀뷰안에 다른 기본뷰들을넣으려면 어떻게 해야할까요??

 

도와주세요 !!

 

 

 

+

문제가 있는거같은 뷰클래스올립니다

 



public class MyAnimationView extends View {

    private static final int RED = 0xffFF8080;
    private static final int BLUE = 0xff8080FF;
    private static final int CYAN = 0xff80ffff;
    private static final int GREEN = 0xff80ff80;

    public final ArrayList<ShapeHolder> balls = new ArrayList<ShapeHolder>();
    AnimatorSet animation = null;

    public MyAnimationView(Context context) {
        super(context);

        // Animate background color
        // Note that setting the background color will automatically invalidate the
        // view, so that the animated color, and the bouncing balls, get redisplayed on
        // every frame of the animation.
        ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);
        colorAnim.setDuration(3000);
        colorAnim.setEvaluator(new ArgbEvaluator());
        colorAnim.setRepeatCount(ValueAnimator.INFINITE);
        colorAnim.setRepeatMode(ValueAnimator.REVERSE);
        colorAnim.start();
    }
    
    public MyAnimationView(Context context, AttributeSet attrs) {
    	super(context, attrs);



        // Animate background color 
        // Note that setting the background color will automatically invalidate the
        // view, so that the animated color, and the bouncing balls, get redisplayed on
        // every frame of the animation.
        ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);
        colorAnim.setDuration(3000);
        colorAnim.setEvaluator(new ArgbEvaluator());
        colorAnim.setRepeatCount(ValueAnimator.INFINITE);
        colorAnim.setRepeatMode(ValueAnimator.REVERSE);
        colorAnim.start(); 
    }
    
    
    public MyAnimationView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        // Animate background color
        // Note that setting the background color will automatically invalidate the
        // view, so that the animated color, and the bouncing balls, get redisplayed on
        // every frame of the animation.
        /*ValueAnimator colorAnim = ObjectAnimator.ofInt(this, "backgroundColor", RED, BLUE);
        colorAnim.setDuration(3000);
        colorAnim.setEvaluator(new ArgbEvaluator());
        colorAnim.setRepeatCount(ValueAnimator.INFINITE);
        colorAnim.setRepeatMode(ValueAnimator.REVERSE);
        colorAnim.start();*/
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        if (event.getAction() != MotionEvent.ACTION_DOWN &&
                event.getAction() != MotionEvent.ACTION_MOVE) {
            return false;
        }
        ShapeHolder newBall = addBall(event.getX(), event.getY());

        // Bouncing animation with squash and stretch
        float startY = newBall.getY();
        float endY = getHeight() - 200f;
        Log.d("BluetoothChatService","startY : "+startY);
        Log.d("BluetoothChatService","endY : "+endY);
        
        //for changing to vertical bouncing
        float startX = newBall.getX();
        float endX = getWidth() - 50f;
        
        
        
        float h = (float)getHeight();
        float eventY = event.getY();
        int duration = (int)(500 * ((h - eventY)/h));
        /*ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "y", startY, endY);*/
        ValueAnimator bounceAnim = ObjectAnimator.ofFloat(newBall, "x", startX, endX);
        bounceAnim.setDuration(duration);
        bounceAnim.setInterpolator(new AccelerateInterpolator());
        /*ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "x", newBall.getX(),
                newBall.getX() - 25f);*/
        ValueAnimator squashAnim1 = ObjectAnimator.ofFloat(newBall, "y", newBall.getY(),
                newBall.getY() - 25f);
        squashAnim1.setDuration(duration/4);
        squashAnim1.setRepeatCount(1);
        squashAnim1.setRepeatMode(ValueAnimator.REVERSE);
        squashAnim1.setInterpolator(new DecelerateInterpolator());
        ValueAnimator squashAnim2 = ObjectAnimator.ofFloat(newBall, "height", newBall.getHeight(),
                newBall.getHeight() + 50);
        squashAnim2.setDuration(duration/4);
        squashAnim2.setRepeatCount(1);
        squashAnim2.setRepeatMode(ValueAnimator.REVERSE);
        squashAnim2.setInterpolator(new DecelerateInterpolator());
        ValueAnimator stretchAnim1 = ObjectAnimator.ofFloat(newBall, "x", endX,
                endX + 25f);
        stretchAnim1.setDuration(duration/4);
        stretchAnim1.setRepeatCount(1);
        stretchAnim1.setInterpolator(new DecelerateInterpolator());
        stretchAnim1.setRepeatMode(ValueAnimator.REVERSE);
        ValueAnimator stretchAnim2 = ObjectAnimator.ofFloat(newBall, "width",
                newBall.getWidth(), newBall.getWidth() - 25);
        stretchAnim2.setDuration(duration/4);
        stretchAnim2.setRepeatCount(1);
        stretchAnim2.setInterpolator(new DecelerateInterpolator());
        stretchAnim2.setRepeatMode(ValueAnimator.REVERSE);
        ValueAnimator bounceBackAnim = ObjectAnimator.ofFloat(newBall, "x", endX,
                startX);
        bounceBackAnim.setDuration(duration);
        bounceBackAnim.setInterpolator(new DecelerateInterpolator());
        // Sequence the down/squash&stretch/up animations
        AnimatorSet bouncer = new AnimatorSet();
        bouncer.play(bounceAnim).before(squashAnim1);
        bouncer.play(squashAnim1).with(squashAnim2);
        bouncer.play(squashAnim1).with(stretchAnim1);
        bouncer.play(squashAnim1).with(stretchAnim2);
//        bouncer.play(bounceBackAnim).after(stretchAnim2);

        // Fading animation - remove the ball when the animation is done
        ValueAnimator fadeAnim = ObjectAnimator.ofFloat(newBall, "alpha", 1f, 0f);
        fadeAnim.setDuration(250);
        fadeAnim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                balls.remove(((ObjectAnimator)animation).getTarget());

            }
        });

        // Sequence the two animations to play one after the other
        AnimatorSet animatorSet = new AnimatorSet();
        animatorSet.play(bouncer).before(fadeAnim);

        // Start the animation
        animatorSet.start();

        return true;
    }

 

bombermin (2,520 포인트) 님이 2014년 9월 27일 질문
bombermin님이 2014년 9월 27일 수정
만드신 class 를 올려야 머가 문제인지 알 수 있을 듯 한데 말이죠?
올렸습니다  애니매이션 클래스에요~

1개의 답변

0 추천
 
채택된 답변
View 안에 View를 넣으려니 안되겠죠.

Layout 같은 ViewGroup 이어야만 View를 child로 가질수 있겠죠.

에러도 그렇게 말해주고 있네요.
아픔제로 (3,460 포인트) 님이 2014년 9월 29일 답변
bombermin님이 2014년 10월 1일 채택됨
...