2010年10月6日 星期三

Android-graphic

一 概說
Android的 2D圖畫典型有兩種方式: 不論(1)或(2) 最終都是將這個view 加入一個layout 或是 直接將這個view 指定給activity 作為contenview---setContentView();
(1).定義 graphic 元件(某些drawable, animation) 進入在 layout 中的 view(background) or 直接imageView.
(2).直接在 view 物件的 draw() 中作畫.

其中(1)適合靜態的顯示圖畫(grachic)或是事先定義好的動畫(android animation物件). (2)適合複雜自訂的一些動畫. 且需要定時 redraw 這個view. 一些 video game 應該都是用這個方法.

(2)中又有兩種方式. 同一個 thread (invalidate)使用一般的 view 元件.  兩個 thread使用 SurfaceView

一般的view元件: 不需要很快的畫面更新速度時使用.
SurfaceView元件:是 view 物件的一種特殊子類別. 其特點是系統開第二個 thread 去處理畫面的圖畫更新. 所以在主程式裡, 不必等主程序處理到ondraw時才更新畫面.



二. Drawables
1.Creating from resource images
建立 imageView 的方式:(在程式立建立)

LinearLayout mLinearLayout;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Create a LinearLayout in which to add the ImageView
    mLinearLayout = new LinearLayout(this);

    // Instantiate an ImageView and define its properties
    ImageView i = new ImageView(this);
    i.setImageResource(R.drawable.my_image);
    i.setAdjustViewBounds(true); // set the ImageView bounds to match the Drawable's dimensions
    i.setLayoutParams(new Gallery.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

    // Add the ImageView to the layout and set the layout as the content view
    mLinearLayout.addView(i);
    setContentView(mLinearLayout);
}

建立 imageView 的方式:(在 Layout.xml 建立)

Resources res = mContext.getResources();
Drawable myImage = res.getDrawable(R.drawable.my_image);

2. Creating from resource XML
直接用xml定義 drawable. 將xml 放在目錄 "res/drawable/" 下.

<transition xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:drawable="@drawable/image_expand">
  <item android:drawable="@drawable/image_collapse">
</transition>

在程式中instantiate 這個 drawable將它放入 ImageView


Resources res = mContext.getResources();
TransitionDrawable transition = (TransitionDrawable) res.getDrawable(R.drawable.expand_collapse);
ImageView image = (ImageView) findViewById(R.id.toggle_image);
image.setImageDrawable(transition);

transition.startTransition(1000);

三 ShapeDrawable
提供內建各種形狀的畫圖物件. 自己本身就有 draw()  method, 可以在自己定義的 view物件內ondraw 使用. 或是直接設定為view的背景 setBackgroundDrawable().

public class CustomDrawableView extends View {
    private ShapeDrawable mDrawable;

    public CustomDrawableView(Context context) {
        super(context);

        int x = 10;
        int y = 10;
        int width = 300;
        int height = 50;

        mDrawable = new ShapeDrawable(new OvalShape());
        mDrawable.getPaint().setColor(0xff74AC23);
        mDrawable.setBounds(x, y, x + width, y + height);
    }

    protected void onDraw(Canvas canvas) {
        mDrawable.draw(canvas);
    }
}


<com.example.shapedrawable.CustomDrawableView
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    />


CustomDrawableView mCustomDrawableView;
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mCustomDrawableView = new CustomDrawableView(this);
    
    setContentView(mCustomDrawableView);
}
四 tweenanimation

        Resources res = getResources();
        Drawable orangebar = res.getDrawable(R.drawable.orangebar);
        iv.setImageDrawable(orangebar);
        AbsoluteLayout.LayoutParams layoutParams02 = new   AbsoluteLayout.LayoutParams(17,227,277,110);        
        iv.setLayoutParams(layoutParams02);
                
        // 動畫設定 (指定比例動畫) (startXScale, endXScale, startYScale, endYScale)
        am = new ScaleAnimation( 1, 1, 0, 1 );        
        // 動畫開始到結束的執行時間 (1000 = 1 秒)
        am.setDuration( 2000 );        
        // 動畫重複次數 (-1 表示一直重複)
        am.setRepeatCount( 0 );          
        // 圖片配置動畫
        iv.setAnimation(am); 
        am.startNow();


當動畫結束觸發事件:

        am.setAnimationListener(new android.view.animation.Animation.AnimationListener(){
         public void onAnimationEnd(Animation animation){
                Intent summain = new Intent();
                summain.setClass(main.this,SumMain.class);
                summain.putExtra("sumcountrylist", sumcountrylist);
                summain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
         startActivity(summain);
         //overridePendingTransition(android.R.anim.fade_in,android.R.anim.fade_out);
         }
public void onAnimationRepeat(Animation arg0) {
// TODO Auto-generated method stub
}
public void onAnimationStart(Animation arg0) {
// TODO Auto-generated method stub
}      
        });

沒有留言:

張貼留言