증가 / 누름시 ImageButton의 축적을 변경하는 방법
나는이 ImageButton
내 응용 프로그램에서 나는 버튼이 필요할 때 이미지를 변경합니다 pressed/focused
. 나는이 ImageButton
의를 제공하는 설정을 src
같은 다음 XML 파일에서 :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- pressed -->
<item
android:state_pressed="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- focused -->
<item
android:state_focused="true"
android:tint="@color/black"
android:drawable="@drawable/search"
/>
<!-- default -->
<item
android:tint="@null"
android:drawable="@drawable/search"
/>
</selector>
를 그러나 ImageButton
누르거나 초점을 맞출 때 색조가 적용되지 않습니다 . 이미지는 곧 표시됩니다. 검은 색은 #000000
언제나처럼 정의 됩니다. 어떤 아이디어?
다음을 통해 코드에서 매우 쉽게 사용할 수 있습니다.
ImageButton button = (ImageButton) this.findViewById(R.id.button_i_want_to_modify);
button.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
도움이 되셨기를 바랍니다.
JS
다음은 xml을 사용하여 수행하는 방법입니다. 드로어 블 폴더에서 선택기를 만듭니다. 예 : touch_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- State when a row is being pressed, but hasn't yet been activated (finger down) -->
<item android:state_pressed="true" android:color="@color/semi_slate" />
<!-- When the view is "activated". In SINGLE_CHOICE_MODE, it flags the active row
of a ListView -->
<item android:state_activated="true" android:color="@color/semi_slate" />
<!-- Default, "just hangin' out" state. -->
<item android:color="@android:color/transparent" />
</selector>
xml의 이미지보기에서 android : tint 속성을 위에서 만든 드로어 블로 설정했습니다.
android:tint = "@drawable/touch_selector"
전체 코드는 다음과 가변됩니다.
<?xml version="1.0" encoding="utf-8"?>
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poster"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:tint="@drawable/touch_selector" />
이것은 프레스 또는 활성화시 ImageView에 응용을 적용하는 모든 xml 솔루션입니다. ImageButton에 유사하게 수행 할 수 있습니다.
이것은 API 레벨> = 21에서만 작동합니다.
xml에서 수행하는 방법을 찾았습니다 (적어도 api 21 이상).
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
<bitmap
android:src="@drawable/search"
android:tint="@color/black"
/>
</item>
<item android:drawable="@drawable/search"/>
</selector>
비트 맵에 사용을 설정하면 터치를 가로 채거나 ImageView 또는 ImageButton을 하위 클래스로 만들 수 있습니다. xml에서 동일한 드로어 블을 추가 할 수 있습니다.
선택기가 생성되면 ImageView 또는 ImageButton의 src로 적용하십시오.
마지막으로 API <21에 대한 솔루션을 찾았습니다.
Button more = (Button) findViewById(R.id.more);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
more.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
} else {
Drawable wrapDrawable = DrawableCompat.wrap(more.getBackground());
DrawableCompat.setTint(wrapDrawable, color));
more.setBackgroundDrawable(DrawableCompat.unwrap(wrapDrawable));
}
이 누군가가 2 시간을 잃지 않도록 도울 수 있습니다!
bt.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
bt.setColorFilter(Color.argb(255, 255, 255, 255)); // White Tint
return true; // if you want to handle the touch event
case MotionEvent.ACTION_UP:
bt.clearColorFilter(); // White Tint
return true; // if you want to handle the touch event
}
return false;
}
});
내가하는 일은 setColorFilter 함수가있는 사용자 정의 버튼을 추가하는 것입니다.
이와 같이 xml의 새 버튼을 사용할 수 있습니다.
public class CustomButton extends Button {
public CustomButton(Context context) {
super(context);
}
public CustomButton(Context context, AttributeSet attributes) {
super(context, attributes);
};
@Override
public boolean onTouchEvent(MotionEvent event) {
int maskedAction = event.getActionMasked();
if (maskedAction == MotionEvent.ACTION_DOWN)
getBackground().setColorFilter(Color.argb(150, 155, 155, 155), PorterDuff.Mode.DST_IN);
else if (maskedAction == MotionEvent.ACTION_UP)
getBackground().setColorFilter(null);
return super.onTouchEvent(event);
}}
그리고 ImageButton의 경우
public class CustomImageButton extends ImageButton {
public CustomImageButton(Context context) {
super(context);
}
public CustomImageButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int maskedAction = event.getActionMasked();
if (maskedAction == MotionEvent.ACTION_DOWN)
setColorFilter(Color.argb(150, 155, 155, 155), PorterDuff.Mode.DST_IN);
else if (maskedAction == MotionEvent.ACTION_UP)
setColorFilter(null);
return super.onTouchEvent(event);
}}
XML 로이 작업을 수행하는 방법을 알고 싶어하는 사람들에게 여기에 몇 가지 요청이 있음을 알았습니다. 사실 아주 간단합니다. 이layer-list
버튼의 드로어 블 (drawable / some_button.xml) :
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/some_button_highlighted" />
<item android:drawable="@drawable/some_button_image" />
</selector>
그리고 이것은 강조 표시된 드로어 블입니다 (drawable / some_button_highlighted.xml).
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/some_button_image"/>
<item>
<shape>
<solid android:color="@color/highlighted_button_color" />
</shape>
</item>
</layer-list>
이제 다른 xml에서 이것을 사용할 수 있습니다.
...
android:drawable="@drawable/some_button"
...
나는 이것이 미래의 누군가에게 도움이되기를 바랍니다.
xml에서 색상 (색조)을 설정할 수 있습니다.
다음 사용을 위해 transparent
( android:background="@null"
)를 설정 background
합니다 tint
.
<ImageButton
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:tint="@color/Amber_200"
android:background="@null"
android:src="@drawable/back_selector" />
선택기를 ImageButton의 src로 정의했듯이 Android는 src 유형과 일치하는 드로어 블을 AFAIK합니다. 따라서 색조는 사용되지 않습니다.
그럼에도 불구하고 비슷한 문제가있었습니다. 당신과 같은 선택기를 사용했지만 android : src 대신 ImageButton의 android : tint 값을 사용하려고했습니다. 물론 선택기에있는 색조 값은 생략했습니다. 모든 상태에 동일한 드로어 블을 사용하기를 원하기 때문에 이것은 문제도 해결할 것입니다. 흥미롭게도 시스템이 'res / color / tint_selector.xml'(실제로 내 선택자)을 정수로 구문 분석 할 수 없다는 NumberFormatException이 매번 발생합니다. 구체적으로 내 코드는 다음과 같습니다.
이것은 /res/color/tint_selector.xml에 저장된 내 선택기입니다.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#D3D3D3"/> <!-- pressed -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
그리고 이것은 해당 ImageButton입니다.
<ImageButton android:id="@+id/program_help"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/symbol"
android:tint="@color/tint_selector">
</ImageButton>
현재는 작동하지 않지만 도움이 될 수 있습니다.
'ProgramingTip' 카테고리의 다른 글
읽기 메모리 장벽과 인증서를 이해하는 방법 (0) | 2020.12.29 |
---|---|
Java에서 제네릭 유형 인스턴스화 (0) | 2020.12.29 |
예외 처리를 위해 printStackTrace ()를 사용하는 것이 나쁜 생각입니까? (0) | 2020.12.29 |
메뉴에 추가하지 않고 WordPress (0) | 2020.12.29 |
Ruby on Rails 3 :“수퍼 클래스 불일치 ...” (0) | 2020.12.29 |