반응형

안드로이드에서 레트로핏을 활용해 서버와 통신하는데 String 데이터를 주고받는 경우

다음과 같이 따옴표가 포함되어 전송되는 문제가 발생했다.



 

알고보니 이 문제는 레트로핏 서비스 인터페이스 작성시에 multipart를 이용하여 통신하는 경우

매개변수를 RequestBody로 하지 않고 String으로 두어 따옴표가 생겨서 전송되는 것이었다.

 

매개변수를 RequestBody로 두고 request 메시지를 보내면 따옴표가 포함되지 않고 텍스트만 전송이 가능하다.

 

 

 

반응형
반응형

lottie 활용해서 좋아요 버튼 만들기

 

Lottie Docs

 

http://airbnb.io/lottie/#/android?id=animation-listeners 

 

Lottie Docs

 

airbnb.io

 

좋아요 애니메이션 json 파일

https://lottiefiles.com/89777-like

 

 

 

lottie를 활용하려면 우선 build.gradle에 dependency에 lottie를 추가해주어야 한다.

dependencies {
    implementation "com.airbnb.android:lottie:5.2.0"
}

 

그리고 app 디렉토리에 assets 폴더를 만들고

다운로드 받은 lottie json 파일을 추가한다.

그리고 추가할 xml 파일에 LottieAnimationView를 추가하고

lottie_fileName에 다운로드 받은 로티 파일의 이름을 추가하면 xml에 로티파일을 추가할 수 있다.

 

 

 

로티 애니메이션을 커스텀 하려면 다음과 같은 방식으로 설정할 수 있다.

ofFloat는 애니메이션 전체의 어떤 부분을 보여줄 것인지 정할 수 있고

setDuration은 정한 부분을 몇초 동안 보여줄 것인지 정할 수 있다.

1초는 1000이다.

val animator = ValueAnimator.ofFloat(0f, 0.5f).setDuration(1000)
animator.addUpdateListener { animation : ValueAnimator ->
    like_btn.setProgress(
        animation.getAnimatedValue() as Float
    )
}
animator.start()

 

 

 

전체코드

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@android:color/white"
    tools:context=".MainActivity">

    <com.airbnb.lottie.LottieAnimationView
        android:id="@+id/like_btn"
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:background="@android:color/white"
        app:lottie_autoPlay="false"
        app:lottie_fileName="like.json"
        app:lottie_loop="false" />
</RelativeLayout>

 

 

MainActivity.kt

package com.example.lottieanimationtutorial

import android.animation.ValueAnimator
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import com.example.lottieanimationtutorial.databinding.ActivityMainBinding
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {
    val TAG = "MainActivity"

    var isLiked: Boolean = false
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        isLiked = false
        //좋아요 버튼에 클릭 리스너를 달아준다.
        like_btn.setOnClickListener {
            if(isLiked == false){
                val animator = ValueAnimator.ofFloat(0f, 0.5f).setDuration(1000)
                animator.addUpdateListener { animation : ValueAnimator ->
                    like_btn.setProgress(
                        animation.getAnimatedValue() as Float
                    )
                }
                animator.start()
                isLiked = true
            }else{ // 좋아요 상태일 때
                val animator = ValueAnimator.ofFloat(0.5f, 1f).setDuration(1000)
                animator.addUpdateListener { animation : ValueAnimator ->
                    like_btn.setProgress(
                        animation.getAnimatedValue() as Float
                    )
                }
                animator.start()
                isLiked = false
            }
        }

    }
}
반응형
반응형

안드로이드 레이아웃 종류

안드로이드에서 자주 쓰는 레이아웃은 크게 3개가 존재한다.

LinearLayout, RelativeLayout,  ConstraintLayout이 있다.

 

LinearLayout은 horizontal이나 vertical 설정으로 편하게 레이아웃을 구성할 수 있다.

weightSum을 이용하면 웹의 css에서 flex와 같은 역할을 할 수 있다.

 

RelativeLayout은 뷰 id를 통해서 상대적 위치를 기준으로 뷰의 위치를 정해줄 수 있다.

또한 RelativeLayout을 이용하면 뷰를 겹쳐서 둘 수 있다.

 

ConstraintLayout은 앵커를 3개 두어 위치를 유동적으로 설정할 수 있다.

 

 

1. LinearLayout

<?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"
    tools:context=".MainActivity"
    android:orientation="horizontal"
    android:weightSum="3"
    android:gravity="center_vertical"
    >

    <View
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#FF0000"
        />
    <View
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#FFF200"
        />
    <View
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:background="#000DFF"
        />


</LinearLayout>

 

2. RelativeLayout

 

RelativeLayout

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
    tools:context=".RelativeLayout">

    <View
        android:layout_width="match_parent"
        android:layout_height="100dp"
        android:background="#FF8F00"
        />


    <View
        android:id="@+id/blue_view"
        android:layout_alignParentLeft="true"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="@color/design_default_color_primary"
        />

    <View
        android:id="@+id/green_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FD62FF00"
        android:layout_toRightOf="@+id/blue_view"
        />

</RelativeLayout>

 

 

3. ConstraintLayout

 

ConstraintLayout

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".ConstraintLayout">

    <View
        android:id="@+id/orange_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#F9A825"
        />
    <View
        android:id="@+id/blue_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toBottomOf="@id/orange_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#1984FF"
        />

    <View
        android:id="@+id/pink_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FD18F2"
        app:layout_constraintTop_toBottomOf="@id/blue_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

    <View
        android:id="@+id/yellow_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#FFF200"
        app:layout_constraintTop_toBottomOf="@id/pink_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />
    <View
        android:id="@+id/green_view"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:background="#27FD18"
        app:layout_constraintTop_toBottomOf="@id/yellow_view"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>
반응형

+ Recent posts