当前位置: 编程技术>移动开发
本页文章导读:
▪自定义 Theme 改变 系统大局样式 自定义 Theme 改变 系统全局样式
转自:http://www.androidworks.com/changing-the-android-edittext-ui-widget Changing the Android EditText UI Widget Summary: This article should be useful to people who want to customize the default UI EditTe.........
▪ Frame卡通片 Frame动画
本例完全参照androidAPI文档上面来写的,哈哈。 步骤: 1. 在res目录下的drawable目录下创建frame-by-frame animation xml文件。 2. 在这个xml文件中只有两个元素,一个是<animation-list.........
▪ Animation(2) Animation(二)
本篇介绍如何用配置文件进行控件的动画设置。步骤如下: 1. 在res目录下建立anim目录 2. 在anim目录下创建动画的xml文件 3. 通过AnimationUtils这个类加载动画的xml文件 4..........
[1]自定义 Theme 改变 系统大局样式
来源: 互联网 发布时间: 2014-02-18
自定义 Theme 改变 系统全局样式
转自:http://www.androidworks.com/changing-the-android-edittext-ui-widget
Changing the Android EditText UI Widget
Summary:
This article should be useful to people who want to customize the default UI EditText as well as TextView on the Android platform. Mostly, I mean the Orange skin that appears to be hard to change. No matter how many color properties I attempted, I failed. Then, after inspecting the Android source code, picking apart how they wrote the TextView control (which EditText extends), I realized it was just a skin of NinePatchdrawables set in the background of the underlying View class. I’ll take you through the steps.
Some background on how it works:
First lets look at the art that Android uses, and how they reference it. This provides a better understanding of what we need to do on our own.
Look in the <android_sdk>\platforms\android-x.x\data\res\drawable directory. In this directory, you will notice this file, edit_text.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected" />
<item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected" />
<item android:drawable="@drawable/textfield_disabled" />
</selector>This is the ColorStateList that is default for the EditText background. This file points to various background resources in each state. So, from inspection, it appears that we need to create NinePatch art for the following drawable files:
textfield_default.9.png, textfield_disabled.9.png, textfield_pressed.9.png, textfield_selected.9.png, textfield_disabled_selected.9.png, etc…
So, this is where and how Android’s default EditText gets the Orange look!
Create your own NinePatch skins:
So change the look to your requirements (using gimp, photoshop, or Android’s recommended Draw9Patch tool), in my case I just did a red version of these files. Place these new png’s in your res/drawable directory. Now they can be referenced by your very own ColorStateList.
Name them .9.png
Create your new ColorStateList
Using the example provided by the default Android edit_text.xml above, create your own version of it, pointing to your own NinePatch files. This file should live in your res/drawable directory also. Now you have a valid ColorStateList visible to styles and widgets.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/textfield_disabled_red" />
<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed_red" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected_red" />
<item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected_red" />
<item android:drawable="@drawable/textfield_disabled_red" />
</selector>Save it as red_edit_text.xml and place in your res/drawable directory.
Point you EditText background to the ColorStateList
In my case, decided to use a theme and style approach to override all EditText boxes in my Application.
From my AndroidManifest.xml application element, set the theme
android:theme="@style/mytheme"From my theme.xml
<resources>
<style name="mytheme" parent="@android:style/Theme" >
<item name="android:editTextStyle">@style/red_edittext</item>
</style>
</resources>
From my styles.xml
<resources>
<style name="red_edittext" parent="@android:style/Widget.EditText">
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:clickable">true</item>
<item name="android:background">@drawable/red_edit_text</item>
<item name="android:textColor">@color/state_list</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textColorHint">@color/default_text_color</item>
<item name="android:textColorHighlight">@color/transparent_red</item>
</style>
<style name="droiddate_btn" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default_red</item>
</style>
</resources>Results:
So now all EditText boxes should have my new red color instead of the default Orange. Although this might not be your end goal, just to change an EditText box from orange to red, it allows you to see how Android NinePatch, ColorStateList , Styles, and Themes can all work together to override and skin any control in Android. It could obviously be much more dramatic than my example below. Good luck, here is the result:
Please feel free to comment,
Marcus Williford
mwilliford@androidworks.com
Tags: Android, Android UI, ColorStateList, EditText, Style, Theme
转自:http://www.androidworks.com/changing-the-android-edittext-ui-widget
Changing the Android EditText UI Widget
Summary:
This article should be useful to people who want to customize the default UI EditText as well as TextView on the Android platform. Mostly, I mean the Orange skin that appears to be hard to change. No matter how many color properties I attempted, I failed. Then, after inspecting the Android source code, picking apart how they wrote the TextView control (which EditText extends), I realized it was just a skin of NinePatchdrawables set in the background of the underlying View class. I’ll take you through the steps.
Some background on how it works:
First lets look at the art that Android uses, and how they reference it. This provides a better understanding of what we need to do on our own.
Look in the <android_sdk>\platforms\android-x.x\data\res\drawable directory. In this directory, you will notice this file, edit_text.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false" android:drawable="@drawable/textfield_disabled" />
<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected" />
<item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected" />
<item android:drawable="@drawable/textfield_disabled" />
</selector>This is the ColorStateList that is default for the EditText background. This file points to various background resources in each state. So, from inspection, it appears that we need to create NinePatch art for the following drawable files:
textfield_default.9.png, textfield_disabled.9.png, textfield_pressed.9.png, textfield_selected.9.png, textfield_disabled_selected.9.png, etc…
So, this is where and how Android’s default EditText gets the Orange look!
Create your own NinePatch skins:
So change the look to your requirements (using gimp, photoshop, or Android’s recommended Draw9Patch tool), in my case I just did a red version of these files. Place these new png’s in your res/drawable directory. Now they can be referenced by your very own ColorStateList.
Name them .9.png
Create your new ColorStateList
Using the example provided by the default Android edit_text.xml above, create your own version of it, pointing to your own NinePatch files. This file should live in your res/drawable directory also. Now you have a valid ColorStateList visible to styles and widgets.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="@drawable/textfield_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="@drawable/textfield_disabled_red" />
<item android:state_pressed="true" android:drawable="@drawable/textfield_pressed_red" />
<item android:state_enabled="true" android:state_focused="true" android:drawable="@drawable/textfield_selected_red" />
<item android:state_enabled="true" android:drawable="@drawable/textfield_default" />
<item android:state_focused="true" android:drawable="@drawable/textfield_disabled_selected_red" />
<item android:drawable="@drawable/textfield_disabled_red" />
</selector>Save it as red_edit_text.xml and place in your res/drawable directory.
Point you EditText background to the ColorStateList
In my case, decided to use a theme and style approach to override all EditText boxes in my Application.
From my AndroidManifest.xml application element, set the theme
android:theme="@style/mytheme"From my theme.xml
<resources>
<style name="mytheme" parent="@android:style/Theme" >
<item name="android:editTextStyle">@style/red_edittext</item>
</style>
</resources>
From my styles.xml
<resources>
<style name="red_edittext" parent="@android:style/Widget.EditText">
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:clickable">true</item>
<item name="android:background">@drawable/red_edit_text</item>
<item name="android:textColor">@color/state_list</item>
<item name="android:gravity">center_vertical</item>
<item name="android:textColorHint">@color/default_text_color</item>
<item name="android:textColorHighlight">@color/transparent_red</item>
</style>
<style name="droiddate_btn" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/btn_default_red</item>
</style>
</resources>Results:
So now all EditText boxes should have my new red color instead of the default Orange. Although this might not be your end goal, just to change an EditText box from orange to red, it allows you to see how Android NinePatch, ColorStateList , Styles, and Themes can all work together to override and skin any control in Android. It could obviously be much more dramatic than my example below. Good luck, here is the result:
Please feel free to comment,
Marcus Williford
mwilliford@androidworks.com
Tags: Android, Android UI, ColorStateList, EditText, Style, Theme
[2] Frame卡通片
来源: 互联网 发布时间: 2014-02-18
Frame动画
本例完全参照androidAPI文档上面来写的,哈哈。
步骤:
1. 在res目录下的drawable目录下创建frame-by-frame animation xml文件。
2. 在这个xml文件中只有两个元素,一个是<animation-list>,另一个<item>,意思一目了然,这里不多解释了。
3. 为控件设置backgroundResource
4. 获取background,并转换成AnimationDrawable
5. 调用AnimationDrawable的start方法来启动动画
话不多说,代码如下:
本例完全参照androidAPI文档上面来写的,哈哈。
步骤:
1. 在res目录下的drawable目录下创建frame-by-frame animation xml文件。
2. 在这个xml文件中只有两个元素,一个是<animation-list>,另一个<item>,意思一目了然,这里不多解释了。
3. 为控件设置backgroundResource
4. 获取background,并转换成AnimationDrawable
5. 调用AnimationDrawable的start方法来启动动画
话不多说,代码如下:
package com.kevin.frame; import android.app.Activity; import android.graphics.drawable.AnimationDrawable; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.ImageView; public class FrameDemo extends Activity { private ImageView img; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.imageView1); // 设置控件的背景资源 img.setBackgroundResource(R.drawable.dance); img.setOnClickListener(new ImgOnClickListner()); } class ImgOnClickListner implements OnClickListener{ @Override public void onClick(View v) { // 获取背景,并转换成AnimationDrawable AnimationDrawable frameAnimation = (AnimationDrawable)img.getBackground(); // 启动动画 frameAnimation.start(); } } }
[3] Animation(2)
来源: 互联网 发布时间: 2014-02-18
Animation(二)
本篇介绍如何用配置文件进行控件的动画设置。步骤如下:
1. 在res目录下建立anim目录
2. 在anim目录下创建动画的xml文件
3. 通过AnimationUtils这个类加载动画的xml文件
4. 给你需要的控件绑定Animation
话不多说,代码如下:
这里举个配置文件说明一下,代码如下:
注意这边的piovtX属性后面跟的值,因为scale缩放时,轴心的位置相对于控件的位置有三种:
1. 当你写50时,它是采用的absolute去取轴心的位置
2. 当你写50%时,它是采用的RELATIVE_TO_SELF去取轴心的位置
3. 当你写50%p时,它是采用的RELATIVE_TO_PARENT去取轴心的位置
好了就写到这里吧。
本篇介绍如何用配置文件进行控件的动画设置。步骤如下:
1. 在res目录下建立anim目录
2. 在anim目录下创建动画的xml文件
3. 通过AnimationUtils这个类加载动画的xml文件
4. 给你需要的控件绑定Animation
话不多说,代码如下:
package com.kevin.animation; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; public class AnimationDemo extends Activity { private ImageView img; private int flag = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); img = (ImageView)findViewById(R.id.imageView1); img.setOnClickListener(new ImgOnClickListenr()); } class ImgOnClickListenr implements OnClickListener{ @Override public void onClick(View v) { switch (flag) { case 0: // 加载动画的配置文件 Animation alphaAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.alpha); // 调用animation img.startAnimation(alphaAnimation); flag++; break; case 1: Animation rotateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.rotate); img.startAnimation(rotateAnimation); flag++; break; case 2: Animation scaleAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.back_scale); img.startAnimation(scaleAnimation); flag++; break; case 3: Animation translateAnimation = AnimationUtils.loadAnimation(AnimationDemo.this, R.anim.translate); img.startAnimation(translateAnimation); flag = 0; default: break; } } } }
这里举个配置文件说明一下,代码如下:
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android"> <scale android:fromXScale="0.0" android:toXScale="1.0" android:fromYScale="1.0" android:toYScale="1.0" android:pivotX="50%" android:pivotY="50%" android:duration="5000"/> </set>
注意这边的piovtX属性后面跟的值,因为scale缩放时,轴心的位置相对于控件的位置有三种:
1. 当你写50时,它是采用的absolute去取轴心的位置
2. 当你写50%时,它是采用的RELATIVE_TO_SELF去取轴心的位置
3. 当你写50%p时,它是采用的RELATIVE_TO_PARENT去取轴心的位置
好了就写到这里吧。
最新技术文章: