读书人

转载:Andorid小目之-Animation4

发布时间: 2012-09-10 11:02:32 作者: rapoo

转载:Andorid小目之--Animation四的片效果(附源)
上篇未述完的例效果,本篇全部android小型的效果,次了方便我有用xml文件加,如果追求OO原可以自行建XML,根具自行配置。

四效果行:




Alpha

拉伸由大到小Scale

移位Translate

旋Rotate
  本篇重:定Gallery件的性信息,通在res\Values\attrs.xml文件定,代如下:

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Gallery">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>



以上XML性信息置了Gallery件的背景格

注意:XML一定要放在values下,如果它放在layout目下,法取;

如果置Gallery,我在此就不多了,您可以在我之前的博客得相的操作方法:http://www.cnblogs.com/TerryBlog/archive/2010/05/17/1737789.html

在里重要的就是以下段代 :

片段一:

代码
private int mGalleryItemBackGround; //取源ID
public static int[] myImageArray=
{
R.drawable.one,
R.drawable.two,
R.drawable.three,
R.drawable.four,
R.drawable.five,
R.drawable.six
};
private Context mContext;
private int Height;
private int Width;
public ImageAdapter(Context c,int height,int width)
{
mContext=c;
Height=height;
Width=width;
TypedArray ta=c.obtainStyledAttributes(R.styleable.Gallery);
mGalleryItemBackGround=ta.getResourceId(R.styleable.Gallery_android_galleryItemBackground,
0);

ta.recycle();
}


造函面,了一上下文,一高度,一度。

一:了防止误“The method obtainStyledAttributes(int[]) is undefined for the type ImageAdapter”,意思是obtainStyledAttributes()是Context类中的方法,如果将ImageAdapter单独写在一个.java文件中的话,必须在obtainStyledAttributes()方法前加上方法的引用才能解决问题。

二三:是了後面置片的高度和度,後方有介。

代示意:通TypedArray或者Gallery件性,然後置Gallery的背景格,之后通TypedArray所得到的象,置Gallery件限循示。

片段二:

代码
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv=new ImageView(mContext);
iv.setImageResource(myImageArray[position]);

iv.setScaleType(ImageView.ScaleType.FIT_CENTER); //置片的高

iv.setLayoutParams(new Gallery.LayoutParams(Width, Height)); //置片的高

iv.setBackgroundResource(mGalleryItemBackGround); //通 上面置的源ID置背景
return iv;
}


重getView方法,置片的源,之后通setLayoutParams方法用的高度和度置片高。

如何取屏幕的高度和度:



import android.util.DisplayMetrics; //命名空

DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);




之后通dm.heightPixels,dm.widthPixels 的性可得到屏幕高度和度。

基本工作完,在到了我的段,四定如下:

代码


private Animation myAnimationAlpha;
private Animation myAnimationScale;
private Animation myAnimationTranslate;
private Animation myAnimationRotate;

myAnimationAlpha=new AlphaAnimation(0.1f, 1.0f);
myAnimationAlpha.setDuration(3000);

myAnimationScale=new ScaleAnimation(0.0f, 1.4f, 0.0f, 1.4f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
myAnimationScale.setDuration(3000);

myAnimationTranslate=new TranslateAnimation(30.0f, -80.0f, 30.0f, 300.0f);
myAnimationTranslate.setDuration(3000);

myAnimationRotate=new RotateAnimation(0.0f, +350.0f,
Animation.RELATIVE_TO_SELF,0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
myAnimationRotate.setDuration(3000);




明看里:http://www.cnblogs.com/TerryBlog/archive/2010/05/30/1747311.html

定好了的示方式,就可以在Gallery件上做作了,做一事件,用每一次就行一次,四效果就循交替示各自的魅力

代如下:

代码
myGallery.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated methsod stub
ImageView ig=(ImageView)arg1;
switch (i) {
case 0:
ig.startAnimation(myAnimationAlpha);
i++;
break;
case 1:
ig.startAnimation(myAnimationScale);
i++;
break;
case 2:
ig.startAnimation(myAnimationTranslate);
i++;
break;
case 3:
ig.startAnimation(myAnimationRotate);
i=0;
break;
default:
break;
}


//ImageAdapter.myImageArray[arg2];
}
});




一始看到效果的列表,有,但耐下心看一下,得是有一定的律的,掌握好程以后目上的效果迎刃而解。
转载出:http://www.cnblogs.com/TerryBlog/archive/2010/05/31/1748628.html

读书人网 >移动开发

热点推荐