android基础之drawable

来源:互联网 发布:程序员转正答辩ppt 编辑:程序博客网 时间:2024/06/05 15:27

一、加载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 =<initial setup> ; Bitmap resizedBitmap =         Bitmap.createScaledBitmap(originalBitmap, newWidth, newHeight, false); 
  • 将bitmap转换为drawable
//Convert Bitmap to DrawableDrawable d = new BitmapDrawable(getResources(),bitmap);

二、XML Drawable

  • Sharp Drawable
<?xml version="1.0" encoding="UTF-8"?><shape  xmlns:android="http://schemas.android.com/apk/res/android"  android:shape="rectangle">  <stroke    android:width="2dp"    android:color="#FFFFFFFF" />  <gradient    android:endColor="#DDBBBBBB"    android:startColor="#DD777777"    android:angle="90" />  <corners    android:bottomRightRadius="7dp"    android:bottomLeftRadius="7dp"    android:topLeftRadius="7dp"    android:topRightRadius="7dp" /></shape> 
  • State Drawable
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">  <item android:drawable="@drawable/button_pressed"    android:state_pressed="true" />  <item android:drawable="@drawable/button_checked"    android:state_checked="true" />  <item android:drawable="@drawable/button_default" /></selector> 
  • transition Drawable
<?xml version="1.0" encoding="utf-8"?><transition xmlns:android="http://schemas.android.com/apk/res/android">  <item android:drawable="@drawable/first_image" />  <item android:drawable="@drawable/second_image" /></transition> 
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 ,优点是可以自动按比例缩放到设备的像素密度
<vector xmlns:android="http://schemas.android.com/apk/res/android"     android:height="64dp"     android:width="64dp"     android:viewportHeight="600"     android:viewportWidth="600" >     <group         android:name="rotationGroup"         android:pivotX="300.0"         android:pivotY="300.0"         android:rotation="45.0" >         <path             android:name="v"             android:fillColor="#000000"             android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />     </group> </vector> 
  • Animation Drawable
    可以通过 setBackgroundResource()的方式给view设置animation drawable
<!-- Animation frames are phase*.png files inside the res/drawable/ folder --> <animation-list android:id="@+id/selected" android:oneshot="false">    <item android:drawable="@drawable/phase1" android:duration="400" />    <item android:drawable="@drawable/phase2" android:duration="400" />    <item android:drawable="@drawable/phase3" android:duration="400" /> </animation-list> 
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;  } }  

在布局文件中使用

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    tools:context=".MainActivity" >    <ImageView        android:id="@+id/image"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:layout_centerHorizontal="true"        android:layout_centerVertical="true"        android:contentDescription="TODO" /></RelativeLayout> 
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));  } }  
0 0
原创粉丝点击