나는 안드로이드의 그래픽에 초보자입니다. (안드로이드 프로그래밍에 꽤 익숙 하긴하지만). 내 앱에 "전구"를 표시하려고합니다. 몇 가지 값을 토대로 전구의 "밝기"를 변경하고 싶습니다.Android 그래픽 : 배경이 아닌 단지 이미지의 알파 변경하기
나는 이것을 위해 PNG 이미지를 사용했다.
<View
android:id="@+id/view2"
android:layout_width="59dp"
android:layout_height="65dp"
android:background="@drawable/lightbulb" />
는 그리고 코드는 이것이다 :
final View normalView = findViewById(R.id.view2);
//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
normalView.invalidate();
PNG 파일이 검은 색 전구의 윤곽과 투명한 그래픽입니다. 이제 알파 값을 더 낮게 (0에 가깝게) 설정하면 전체 그래픽이 투명 해집니다. 전구의 내용이 투명 해지기를 원합니다. 전구의 윤곽선이 보이기를 바랍니다.
다음과 같이 내 사용자 지정보기를 만들려고했지만 지금은 그래픽이 전혀 표시되지 않습니다.
class LightbulbView extends View {
private BitmapDrawable mLightbulbDrawable;
private Paint mPaint;
/*All constructors which call through to super*/
private void initialize(){
mLightbulbDrawable = new BitmapDrawable(getResources(), BitmapFactory.decodeResource(getResources(), R.drawable.lightbulb));
mPaint = new Paint();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mLightbulbDrawable.draw(canvas);
}
/*Without this, I get an NPE when I do a getBackground on this view*/
@Override
public Drawable getBackground() {
return mLightbulbDrawable;
//return super.getBackground();
}
}
그리고 물론 ImageView를 사용하면 좋지 않습니다. 결과는 정확히 같습니다.
<ImageView
android:layout_width="wrap_content"
android:id="@+id/imageView1"
android:layout_height="wrap_content"
android:src="@drawable/lightbulb"
></ImageView>
그리고 여기 그래서, 안드로이드에서이 작업을 수행하는 가장 좋은 방법은 무엇입니까 코드
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setAlpha(alphaValue);
입니까?
솔루션
이것은 내가 그것을 한 방법이다. 자세한 내용은 (@Petrus에 의해) 허용 대답을 참조하십시오
<FrameLayout
android:id="@+id/frameLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:layout_height="wrap_content"
android:src="@drawable/lightbulb_content"
android:layout_width="wrap_content"
android:id="@+id/bulbContent"
></ImageView>
<ImageView
android:layout_height="wrap_content"
android:src="@drawable/lightbulb"
android:layout_width="wrap_content"
android:id="@+id/bulb"
></ImageView>
</FrameLayout>
그리고 코드 :
여기final ImageView bulbContent = (ImageView) findViewById(R.id.bulbContent);
//Where I need to set the alpha
int alphaValue = /*Some calculation*/
normalView.getBackground().setAlpha(alphaValue);
bulbContent.setAlpha(alphaValue);
, lightbulb.png는 전구의 단지 개요와 이미지가이다; lightbulb_content.png는 전구의 "내용"이있는 이미지입니다.
Perfect! 그것은 트릭을했다. 감사. – curioustechizen