본문 바로가기
안드로이드 자바

[Java][Android] 안드로이드 vector 이미지 취소선 추가

by teamnova 2022. 3. 19.
728x90

strikedImageView 라이브러리를 사용해서, vector 이미지에 취소선을 추가해보겠습니다.

 

build.gradle(Module)

dependencies {
    ...
    implementation 'com.github.DamonBaker:StrikedImageView:1.0.0'
    ...
    }

build.gradle(Project)

allprojects {
    repositories {
        ...
        maven { url 'https://jitpack.io' }
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:gravity="center"
    android:layout_height="match_parent">

    <xyz.damonbaker.strikedimageview.StrikedImageView
        android:id="@+id/striked_image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:tint="#FFC107"
        android:clickable="true"
        app:srcCompat="@drawable/ic_camera_black_24dp"
        app:striked="false" />


</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

    StrikedImageView strikedImageView;
    Boolean  status = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        strikedImageView = findViewById(R.id.striked_image);

        strikedImageView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!status){
                    strikedImageView.setStriked(true);
                    status = true;
                }
                else{
                    strikedImageView.setStriked(false);
                    status = false;
                }
            }
        });
    }
}

클릭하면 취소선이 생기고 사라집니다.