android基础之drawable

一、加载bitmap 和 drawable

  • 从asserts文件夹创建bitmap,并赋给imageview
    AssetManager manager = getAssets();
 
    // read a Bitmap from Assets 
    InputStream open = null;
    try { 
      open = manager.open("logo.png");
      Bitmap bitmap = BitmapFactory.decodeStream(open);
      // Assign the bitmap to an ImageView in this layout 
      ImageView view = (ImageView) findViewById(R.id.imageView1);
      view.setImageBitmap(bitmap);
    } catch (IOException e) {
      e.printStackTrace();
    } finally { 
      if (open != null) {
        try { 
          open.close();
        } catch (IOException e) {
          e.printStackTrace();
        } 
      } 
    }  
  • 从res/drawable 中获取drawable
    Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.ic_action_search);
  • 基于新的宽高尺寸获取bitmap
Bitmap originalBitmap = ; 
Bitmap resizedBitmap = 
        Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false); 
  • 将bitmap转换为drawable
//Convert Bitmap to Drawable
Drawable d = new BitmapDrawable(getResources(),bitmap);

二、XML Drawable

  • Sharp Drawable


  
  
  
 
  • State Drawable


  
  
  
 
  • transition Drawable


  
  
 
final ImageView image = (ImageView) findViewById(R.id.image);
final ToggleButton button = (ToggleButton) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() { 
  @Override 
  public void onClick(final View v) {
    TransitionDrawable drawable = (TransitionDrawable) image.getDrawable();
    if (button.isChecked()) {
      drawable.startTransition(500);
    } else { 
      drawable.reverseTransition(500);
    } 
  } 
});  
  • Vector drawable
    Android 5.0 开始可以定义Vector drawable ,优点是可以自动按比例缩放到设备的像素密度

     
         
     
  
  • Animation Drawable
    可以通过 setBackgroundResource()的方式给view设置animation drawable

 
    
    
    
  
ImageView img = (ImageView)findViewById(R.id.yourid);
img.setBackgroundResource(R.drawable.your_animation_file);
 
 // Get the AnimationDrawable object. 
 AnimationDrawable frameAnimation = (AnimationDrawable) img.getBackground();
 
 // Start the animation (looped playback by default). 
 frameAnimation.start();
  • 9 Patch drawable

  • 自定义 drawable
    创建custom Drawable类

package com.vogella.android.drawables.custom; 

import android.graphics.Bitmap;
import android.graphics.BitmapShader;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.drawable.Drawable;
 
public class MyRoundCornerDrawable extends Drawable {
 
  private Paint paint;
 
  public MyRoundCornerDrawable(Bitmap bitmap) {
    BitmapShader shader;
    shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP,
        Shader.TileMode.CLAMP);
    paint = new Paint();
    paint.setAntiAlias(true);
    paint.setShader(shader);
  } 
 
  @Override 
  public void draw(Canvas canvas) {
    int height = getBounds().height();
    int width = getBounds().width();
    RectF rect = new RectF(0.0f, 0.0f, width, height);
    canvas.drawRoundRect(rect, 30, 30, paint);
  } 
 
  @Override 
  public void setAlpha(int alpha) {
    paint.setAlpha(alpha);
  } 
 
  @Override 
  public void setColorFilter(ColorFilter cf) {
    paint.setColorFilter(cf);
  } 
 
  @Override 
  public int getOpacity() { 
    return PixelFormat.TRANSLUCENT;
  } 
 
}  

在布局文件中使用



    

 
package com.vogella.android.drawables.custom; 
 
import java.io.InputStream;
 
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ImageView;
 
public class MainActivity extends Activity {
 
  @Override 
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ImageView button = (ImageView) findViewById(R.id.image);
    InputStream resource = getResources().openRawResource(R.drawable.dog);
    Bitmap bitmap = BitmapFactory.decodeStream(resource);
    button.setBackground(new MyRoundCornerDrawable(bitmap));
  } 
 
}  

你可能感兴趣的:(android基础之drawable)