Android: TextView添加图片(drawable)及动画实例

1、首先是界面的UI layout: main.xml(片段)

<Button
  Android:text="@string/start_button"
  android:id="@+id/startButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </Button>
 
 <Button
  android:text="@string/finish_button"
  android:id="@+id/finishButton"
  android:layout_width="wrap_content"
  android:layout_height="wrap_content">
 </Button>
 
 <TextView
  android:id="@+id/statusTextView"
  android:layout_width="fill_parent"
  android:layout_height="70px"
  android:textColor="#FFFFFFFF"
  android:textSize="18px"
  android:paddingLeft="30px"
  android:gravity="center_vertical">
 </TextView>

2、Activity代码:

public class TextViewDemo extends Activity {
 
 private TextView statusTextView;
 private Drawable statusIcon;
 private Drawable arrowIcon;
 private LayerDrawable layerDrawable;
 private Animation anim_start;
 private Animation anim_finish;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        init();
    }
   
    private void init(){
    
     Button startButton = (Button)findViewById(R.id.startButton);
     startButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    setStartStatus();
   }
  });
    
     Button finishButton = (Button)findViewById(R.id.finishButton);
     finishButton.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
    setFinishStstus();
   }
  });
    
     statusTextView = (TextView)findViewById(R.id.statusTextView);
     statusTextView.setCompoundDrawablePadding(10);
     anim_start = AnimationUtils.loadAnimation(this, R.anim.status_view_enter);
     anim_finish = AnimationUtils.loadAnimation(this, R.anim.status_view_enter);
     statusIcon = zoomDrawable(getResources().getDrawable(R.drawable.status_icon),60, 60) ;
     arrowIcon = zoomDrawable(getResources().getDrawable(R.drawable.arrow), 40, 30);
     Drawable[] icons = new Drawable[2];
     icons[0] = statusIcon;
     icons[1] = arrowIcon;
     layerDrawable = new LayerDrawable(icons);
     layerDrawable.setLayerInset(0, 0, 0, 20, 0);
     layerDrawable.setLayerInset(1, 40, 20, 0, 0);
    }
   
    private void setStartStatus(){
     statusTextView.setText(R.string.start_text);
     statusTextView.setCompoundDrawablesWithIntrinsicBounds(layerDrawable.getDrawable(0), null, null, null);
     statusTextView.startAnimation(anim_start);
    
    }
   
    private void setFinishStstus(){
     statusTextView.setText(R.string.finish_text);
     statusTextView.setCompoundDrawablesWithIntrinsicBounds(layerDrawable, null, null, null);
     statusTextView.startAnimation(anim_finish);
    }
   
    private Drawable zoomDrawable(Drawable drawable, int w, int h)
    {
        int width = drawable.getIntrinsicWidth();
        int height= drawable.getIntrinsicHeight();
        // drawable转换成bitmap
        Bitmap oldbmp = drawableToBitmap(drawable);
        // 创建操作图片用的Matrix对象
        Matrix matrix = new Matrix();
        // 计算缩放比例
        float scaleWidth = ((float)w / width);  
        float scaleHeight = ((float)h / height);
       
        // 设置缩放比例
        matrix.postScale(scaleWidth, scaleHeight);
        // 建立新的bitmap,其内容是对原bitmap的缩放后的图
        Bitmap newbmp = Bitmap.createBitmap(oldbmp, 0, 0, width, height, matrix, true);
        // 把bitmap转换成drawable并返回
        return new BitmapDrawable(newbmp);    
    }
   

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wwxwzp.html