2. <set
3. xmlns:Android="http://schemas.android.com/apk/res/android"
4. Android:interpolator="@android:anim/decelerate_interpolator">
<scale Android:fromXScale="2.0" android:toXScale="1.0"
5. Android:fromYScale="2.0" android:toYScale="1.0"
6. Android:pivotX="50%p" android:pivotY="50%p"
7. Android:duration="@android:integer/config_mediumAnimTime" />
</set>
可能有很多人不理解其中的android:interpolator="@android:anim/decelerate_interpolator"是什么含义,文档里说的也不太清楚,其实很简单,看下面:
interpolator定义一个动画的变化率(the rate of change)。这使得基本的动画效果(alpha, scale, translate, rotate)得以加速,减速,重复等。
用通俗的一点的话理解就是:动画的进度使用 Interpolator 控制。Interpolator 定义了动画的变化速度,可以实现匀速、正加速、负加速、无规则变加速等。Interpolator 是基类,封装了所有 Interpolator 的共同方法,它只有一个方法,即 getInterpolation (float input),该方法 maps a point on the timeline to a multiplier to be applied to the transformations of an animation。Android 提供了几个 Interpolator 子类,实现了不同的速度曲线,如下:
AccelerateDecelerateInterpolator 在动画开始与介绍的地方速率改变比较慢,在中间的时侯加速
AccelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始加速
CycleInterpolator 动画循环播放特定的次数,速率改变沿着正弦曲线
DecelerateInterpolator 在动画开始的地方速率改变比较慢,然后开始减速
LinearInterpolator 在动画的以均匀的速率改变
对于 LinearInterpolator ,变化率是个常数,即 f (x) = x.
public float getInterpolation(float input) {
return input;
}
Interpolator其他的几个子类,也都是按照特定的算法,实现了对变化率。还可以定义自己的 Interpolator 子类,实现抛物线、自由落体等物理效果。
1.当你要旋转一个animition时 你会发现如果你只用rotate 它是不平滑的 旋转360度之后它会滞留一会 然后再转 给人感觉是暂停那么一会 怎么消除呢?这是因为如果只用rotate
它默认使用了android:anim/accelerate_interpolator,所以你要写一个自己的interpolator ,这个interpolator就是linearInterpolator。
所以你必须先建一个res/anim/linear_interpolator.xml:
<?xml version="1.0" encoding="utf-8"?><linearInterpolator xmlns:android="http://schemas.android.com/apk/res/android" />
然后在你的animition中加入
android:iterpolator="@anim/linear_interpolator"这句话就可以了
如:
<?xml version="1.0" encoding="UTF-8"?><rotate xmlns:android="http://schemas.android.com/apk/res/android" android:fromDegrees="0" android:toDegrees="360" android:pivotX="50%" android:pivotY="50%" android:repeatCount="infinite" android:duration="1200" />
默认的是accelerate_interpolator,所以你必须在上面的xml中加入
android:iterpolator="@anim/linear_interpolator"
然后就是平滑的旋转了。
2.当采用过滤的方法:
final TextWatcher textChecker = new TextWatcher() {
public void afterTextChanged(Editable s) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void onTextChanged(CharSequence s, int start, int before, int count) {
filterData(search.getText().toString());
}
};
private void filterData(String textinput){
mCursor = mHelperData.searchData(textinput);
startManagingCursor(mCursor);
String[] from = new String[]{ MyData.KEY_ROWID,
MyData.KEY_TITLE, MyData.KEY_DESCRIPTION };
int[] to = new int[]{ R.id.id, R.id.title, R.id.description, R.id.icon };
setListAdapter(new MyAdapter(this, R.layout.list_row, mCursor, from, to ));}
3. 直接发起一个相关的联系人:
Intent intent = new Intent();intent.setAction(Contacts.Intents.SHOW_OR_CREATE_CONTACT);
Intent dialIntent = new Intent( "android.intent.action.DIAL",Uri.parse("tel:666444666")); startActivity(dialIntent);
4.字符串空格以及字符串参数的问题
<string name="Toast_Memory_GameWon_part1">you found ALL PAIRS ! on\ </string><string name="Toast_Memory_GameWon_part2">\ flips !</string>
想在字符串前面和后面有空格一定要注意转义
String message_all_pairs_found = getString(R.string.Toast_Memory_GameWon_part1)+"total_flips"+getString(R.string.Toast_Memory_GameWon_part2);
那么也可以如下实现:
<string name="Toast_Memory_GameWon">you found ALL PAIRS ! on %d flips !</string>
String message_all_pairs_found = String.format(getString(R.string.Toast_Memory_GameWon), total_flips);
5.onKeyDown不起作用 主要因为 可能没有设置setFocusableInTouchMode(true)
最好也加上 setFocusable(true)
6.只知道包名的不知道 R.id的解决办法:
getResources().getIdentifier()
当然你最好吧这个文件放在res/raw/myDataFile 然后读取
7.播放序曲简单方法:
private void playIntro(){ setContentView(R.layout.intro); VideoView video = (VideoView) this.findViewById(R.id.VideoView01); Uri uri = Uri.parse("android.resource://real.app/" + R.raw.intro); video.setVideoURI(uri); video.requestFocus(); video.setOnCompletionListener(this); video.start(); }
8. 用流加载图片
BufferedInputStream buf = new BufferedInputStream(mContext.openFileInput(value));Bitmap bitmap = BitmapFactory.decodeStream(buf);
9.自定义一个dialog
class MyDialog extends AlertDialog {
public MyDialog(Context ctx) {
super(ctx);
LayoutInflater factory = LayoutInflater.from(context);
View view = factory.inflate(R.layout.dialog_layout, null);
setView(view);
setTitle("MyTitle");
setIcon(R.drawable.myicon);
}
}
10. 邮件发送图片
Intent sendIntent = new Intent(Intent.ACTION_SEND);
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
sendIntent.setType("jpeg/image");
sendIntent.putExtra(Intent.EXTRA_EMAIL, "me@gmail.com");
sendIntent.putExtra(Intent.EXTRA_SUBJECT, "aadjfk");
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+ sPhotoFileName));
sendIntent.putExtra(Intent.EXTRA_TEXT, sEmailBody);
startActivity(Intent.createChooser(sendIntent, "Email:"));
一定要注意"file://"是两个斜杠
11. 播放视频 htc和其他大部分手机在播放视频时 有些不同
tostart = new Intent(Intent.ACTION_VIEW);
tostart.setDataAndType(Uri.parse(movieurl), "video/*");
startActivity(tostart);
在大部分手机上是
tostart.setClassName("com.android.camera","com.android.camera.MovieView");
而在htc上
tostart.setClassName("com.htc.album","com.htc.album.ViewVideo");
因此你需要判断
private boolean isCallable(Intent intent) {
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
return list.size() > 0;
}
final Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.camera", "com.android.camera.CropImage");
if (isCallable(intent)) { // call the intent as you intended.}
else { // make alternative arrangements.}