본문 바로가기
학교 강의/모겜프

모겜프 7주차

by hoshi03 2023. 10. 19.

볼륨 컨트롤러 과제

volumecontrollerview.java

package com.example.MGP_LECTURE;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;

public class VolumeControllerView extends ImageView implements View.OnTouchListener {

    private  double angle = 0.0d;
    private knobListener listener;
    float x,y;
    float mx, my;

    public interface knobListener {
        public void onChanged(double angle);
    }

    public void setKnobListner(knobListener lis){
        listener = lis;
    }

    public VolumeControllerView(Context context){
        super(context);
        this.setImageResource(R.drawable.knob);
        this.setOnTouchListener(this);
    }

    public VolumeControllerView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.setImageResource(R.drawable.knob);
        this.setOnTouchListener(this);
    }

    private  double getAngle(float x, float y){
        mx = x - (getWidth() / 2.0f);
        my = (getHeight() / 2.0f) - y;
        double degree = Math.atan2(mx, my) * 180 / 3.141592;
        return degree;
    }

    @Override
    public  boolean onTouch(View v, MotionEvent event){
        x = event.getX(0);
        y = event.getY(0);
        angle = getAngle(x,y);
        invalidate();
        listener.onChanged(angle);
        return  true;
    }

    protected  void onDraw(Canvas c){
        Paint paint = new Paint();
        c.rotate((float) angle, getWidth() / 2, getHeight() / 2);
        super.onDraw(c);
    }

}

mainactivity.java

package com.example.MGP_LECTURE;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.widget.RatingBar;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RatingBar ratingBar = (RatingBar) findViewById(R.id.ratingBar);

        VolumeControllerView view = (VolumeControllerView) findViewById(R.id.volume);
        view.setKnobListner(new VolumeControllerView.knobListener() {
            @Override
            public void onChanged(double angle) {
                float rating = ratingBar.getRating();
                if (angle > 0 && rating < 7.0) ratingBar.setRating(rating + 1.0f);
                else  if (rating > 0.0) ratingBar.setRating(rating - 1.0f);
            }
        });
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <com.example.MGP_LECTURE.VolumeControllerView
        android:id="@+id/volume"
        android:layout_width="300px"
        android:layout_height="300px"
        android:layout_gravity="center" />

    <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/volume"
        android:layout_alignParentStart = "true"
        android:layout_alignParentLeft = "true"
        android:layout_marginStart="13dp"
        android:layout_marginLeft="13dp"
        android:layout_marginTop="36dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</LinearLayout>

 

7주차 곡선그리기는 작성 예제만 보여줌

 

터치로 원 움직이기, mainacitivy만 사용

package com.example.MGP_LECTURE;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RatingBar;

public class MainActivity extends AppCompatActivity {
    protected class MyView extends View{
        int x = 100, y = 100;

        String str;

        public MyView(Context context) {
            super(context);
            setBackgroundColor(Color.YELLOW);
        }

        @Override
        protected void onDraw(Canvas canvas){
            Paint paint = new Paint();
            paint.setColor(Color.RED);
            canvas.drawCircle(x,y,100,paint);
            paint.setTextSize(50);
            canvas.drawText("액션의 종류" + str, 0, 100, paint);
        }

        @Override
        public boolean onTouchEvent(MotionEvent event){
            x = (int) event.getX();
            y = (int) event.getY();

            if (event.getAction() == MotionEvent.ACTION_DOWN) str = "ACTION_DOWN";
            if (event.getAction() == MotionEvent.ACTION_MOVE) str = "ACTION_MOVE";
            if (event.getAction() == MotionEvent.ACTION_UP) str = "ACTION_UP";
            str += " 2019026380 이찬호";
            invalidate();
            return  true;
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyView w = new MyView(this);
        setContentView(w);
    }
}

xj

사자 터치로 확대, 축소 , space + 클릭으로 진행

 

package com.example.MGP_LECTURE;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.RatingBar;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(new MyImageView(this));
    }
}
package com.example.MGP_LECTURE;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ScaleDrawable;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.View;

public class MyImageView extends View {
    private Drawable image;
    private ScaleGestureDetector gestureDetector;
    private float scale = 1.0f;
    public MyImageView(Context context) {
        super(context);
        image = context.getResources().getDrawable(R.drawable.lion);
        setFocusable(true);
        image.setBounds(0,0, image.getIntrinsicWidth(),image.getIntrinsicHeight());
        gestureDetector = new ScaleGestureDetector(context, new ScaleListener());
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        canvas.save();
        canvas.scale(scale,scale);
        image.draw(canvas);
        canvas.restore();
    }

    @Override
    public boolean onTouchEvent(MotionEvent event){
        gestureDetector.onTouchEvent(event);
        invalidate();
        return  true;
    }

    private class ScaleListener extends ScaleGestureDetector.SimpleOnScaleGestureListener{
        @Override
        public boolean onScale(ScaleGestureDetector detector){
            scale *= detector.getScaleFactor();
            if (scale < 0.1f) scale = 0.1f;
            if (scale > 10.0f) scale = 10.0f;
            invalidate();
            return  true;
        }
    }
}

 

터치로 곡선 그리기도 해보기

ppt 21장 까지

'학교 강의 > 모겜프' 카테고리의 다른 글

모겜프 12주차  (0) 2023.11.23
11주차  (1) 2023.11.16
모겜프 10주차  (0) 2023.11.09
모겜프 시험대비  (1) 2023.10.23
시험대비  (0) 2023.10.12