2012-10-05 2 views
0

광선 효과가 잘 작동합니다. 내 의심은 어떻게 광선 효과를 숨기는 것입니까? 내 이미지 뷰를 클릭하면 그 시간에만 내 글로우 효과를 표시하고 싶습니다. 클릭하면 반짝이는 효과를 숨기고 표시하는 방법을 알려주세요.광선 효과를 숨기는 방법?

코드 :

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // An added margin to the initial image 
     int margin = 24; 
     int halfMargin = margin/2; 

     // the glow radius 
     int glowRadius = 16; 

     // the glow color 
     int glowColor = Color.rgb(0, 192, 255); 

     // The original image to use 
     Bitmap src = BitmapFactory.decodeResource(getResources(), 
       R.drawable.test); 

     // extract the alpha from the source image 
     Bitmap alpha = src.extractAlpha(); 

     // The output bitmap (with the icon + glow) 
     Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin, 
       src.getHeight() + margin, Bitmap.Config.ARGB_8888); 

     // The canvas to paint on the image 
     Canvas canvas = new Canvas(bmp); 

     Paint paint = new Paint(); 
     paint.setColor(glowColor); 

     // outer glow 
     paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER)); 
     canvas.drawBitmap(alpha, halfMargin, halfMargin, paint); 

     // original icon 
     canvas.drawBitmap(src, halfMargin, halfMargin, null); 
     setContentView(R.layout.activity_main); 

     ((ImageView) findViewById(R.id.bmpImg)).setImageBitmap(bmp); 

    } 
} 

현재의 스크린 샷 :

enter image description here

+0

R.id.bmpImg. 마지막 줄에 대한 설명을하고 싶습니까? –

+0

@ Mr.Hyde 그것의 아무 것도 내 레이아웃에서 이미지 뷰를 사용하므로 id를 통해 해당 이미지 뷰를 사용하고 있습니다. 그게 다야. 이 형식을 사용할 수있는 또 다른 형식입니다. ImageView img = (ImageView) findViewById (R.id.bmpImg); 2.img.setImageBitmap (bmp); – balaji

답변

2

onclicklistener를 설정하고이 코드를 구현하십시오.

.setOnClickListener(clicklistener); 


private OnClickListener backListener = new OnClickListener() { 
     public void onClick(View v) { 
    // An added margin to the initial image 
     int margin = 24; 
     int halfMargin = margin/2; 

     // the glow radius 
     int glowRadius = 16; 

     // the glow color 
     int glowColor = Color.rgb(0, 192, 255); 

     // The original image to use 
     Bitmap src = BitmapFactory.decodeResource(getResources(), 
       R.drawable.test); 

     // extract the alpha from the source image 
     Bitmap alpha = src.extractAlpha(); 

     // The output bitmap (with the icon + glow) 
     Bitmap bmp = Bitmap.createBitmap(src.getWidth() + margin, 
       src.getHeight() + margin, Bitmap.Config.ARGB_8888); 

     // The canvas to paint on the image 
     Canvas canvas = new Canvas(bmp); 

     Paint paint = new Paint(); 
     paint.setColor(glowColor); 

     // outer glow 
     paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER)); 
     canvas.drawBitmap(alpha, halfMargin, halfMargin, paint); 

     // original icon 
     canvas.drawBitmap(src, halfMargin, halfMargin, null); 
}} 
0

StateListDrawable을 사용하려면이 될 것이라고 할 수있는 쉬운 방법.

일반 이미지 이미지 1 개와 눌린 상태 이미지 1 개 (글로우 포함)를 만듭니다.

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:drawable="@drawable/image_normal" 
     android:state_enabled="true"/> 
    <item 
     android:drawable="@drawable/image_pressed" 
     android:state_pressed="true"/> 
</selector> 

과 buttom의 묘화로 사용 : 입술/드로어 블/사진 button_drawable.xml이 사용이 방법

paint.setMaskFilter(null); 

처럼 setMaskFilter()에 null을 설정할 수 있습니다

((ImageView) findViewById(R.id.bmpImg)).setImageDrawable(getResources().getDrawable(R.drawable.button_drawable)); 
+0

android : drawable = "@ drawable/image_normal", android : drawable = "@ 드로어 블/image_pressed"오류 – balaji

+0

앞서 언급했듯이 이미지 (image_normal.png 및 image_pressed.png)를 만들고 드로어 블 폴더에 추가하십시오. 업데이트 된 XML도 주목하십시오. – Rajesh

+0

ImageView 형의 setImageDrawable (Drawable) 메소드는 arguments (int) 에러 라인에는 적용 할 수 없습니다. ((ImageView) findViewById (R.id.bmpImg)). setImageDrawable (R.drawable.button_drawable); – balaji

2

방금 설정해야합니다 paint.setMaskFilter(new BlurMaskFilter(glowRadius, Blur.OUTER));

이 경우에는 appli 용 페인트 객체를 유지해야합니다. 양이온 범위 그래서이 페인트 객체는 원하는 곳 어디에서나 다른 클래스 나 액티비티에 액세스 할 수 있습니다. 또는 true로 플래그를 설정하고 글로우 효과를 표시하고 false로 플래그를 설정하지 않음 (기본값)

+0

감사합니다. 한 번 더 onclick 이벤트를 사용하는 방법에 의심의 여지가 있습니다. 내 이미지보기 – balaji