打造高质量Android应用读书笔记

来源:互联网 发布:软件项目助理 编辑:程序博客网 时间:2024/05/04 23:17

原书代码:   https://github.com/Macarse/50AH-code

 

Hack1: 最简单的方法实现一个按钮宽度为父布局的一半:

weightSum 和layout_weight 属性

 

Hack5: TextSwitcher ImageSwitcher 文本或图片切换时可以添加动画效果.

     http://wiki.jikexueyuan.com/project/twenty-four-Scriptures/text-switcher-image-switcher.html

 

Hack 6 为viewGroup 的子视图添加悦目的动画效果

使用LayoutAnimationController类

例如: 为ListView 添加出厂动画

1.      创建动画集合对象 加入动画

2.      创建LayoutAnimationController对象,把动画集合对象传入构造方法中

LayoutAnimationControllercontroller = new LayoutAnimationController(set, 0.5f);

3.      mListView.setLayoutAnimation(conreoller);

 

mList= (ListView) findViewById(R.id.lv);

// 设置动画  ListView的条目从上到下依次出现,瀑布效果
AnimationSet set = newAnimationSet(true);
Animation animation = newAlphaAnimation(0.0f,1.0f);
animation.setDuration(200);
set.addAnimation(animation);

animation = newTranslateAnimation(Animation.RELATIVE_TO_SELF,0.0f,
       
Animation.RELATIVE_TO_SELF,0.0f,Animation.RELATIVE_TO_SELF,-1.0f,
       
Animation.RELATIVE_TO_SELF,0.0f);
animation.setDuration(500);
set.addAnimation(animation);

LayoutAnimationController controller = new LayoutAnimationController(set,0.5f);

mList.setLayoutAnimation(controller);

 

 

Hack 7 在Canvas上显示动画

例: 小方块在屏幕上跳动

在onDraw()方法中通过调用invalidate()方法变换视图的位置是实现自定义动画的简单方法.

这个方法是强制重绘视图,把这个方法放在onDraw)_的目的是为了在View绘制完成后,可以立即重新调用onDraw()方法,也就是说,通过循环调用RecTangle的move()和onDraw()方法,实现一个动画效果.

 

Hack10 格式化TextView 的文本

可以通过Html.fromHtml()方法设置TextView的文本内容.该方法将HTML转化为一个Spanned

对象,并以此为参数调用TextView的setText()方法.

另外一种方法,不是用HTML格式化文本内容,而是使用SpannableString类创建一个Spanned对象.这种方法可以实现TextView的部分文字的点击事件,但是这样在个别机型上有长按崩溃的现象.

项目中用到的是在xml中定义文本,

<string name="approve"><![CDATA[        根据监管要求,您需通过实名认证后,才可以进行其他操作。<br>        信息一经认证不能修改。为方便后续操作,所填信息须准确无误。<br>        目前我们不接受来自阿尔及利亚、厄瓜多尔、印度尼西亚、伊朗、朝鲜、缅甸、叙利亚共和国以及美国等国家和地区的用户。        <font color="#2f82c7">查看所有不支持国家</font><br>        企业用户请前往网站进行认证。        ]]></string>
 
mTv.setText(Html.fromHtml(        getResources().getString(R.string.approve)));

 

Hack11: 为文本添加发亮的效果

类似于LED 时钟的样式, 字体设置通过ttf设置

1.      新建assets文件夹,文件夹的的位置必须是和res平级

2.      新建LedTextView 设置字体

    public class LEDTextView extends TextView {    private static final String FONTS_FOLDER = "fonts";    private static final String FONT_DIGITAL_7 = FONTS_FOLDER            + File.separator + "digital-7.ttf";    public LEDTextView(Context context) {        super(context);        init(context);    }    public LEDTextView(Context context, AttributeSet attrs) {        super(context, attrs);        init(context);    }    public LEDTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);        init(context);    }    private void init(Context context) {        AssetManager assets = context.getAssets();        final Typeface font = Typeface.createFromAsset(assets,                FONT_DIGITAL_7);        setTypeface(font);    }}

 

3.      布局文件

<com.ddswez.lpf.myapplication.hack.hack11.LEDTextView    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:text="88:88:88"    android:textColor="#33ff0000"    android:textSize="80sp" /><com.ddswez.lpf.myapplication.hack.hack11.LEDTextView    android:id="@+id/main_clock_time"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:layout_centerInParent="true"    android:shadowColor="#ff0000"    android:shadowDx="0"    android:shadowDy="0"    android:shadowRadius="10"    android:textColor="#ff0000"    android:textSize="80sp" />

 

修改shadowDx  shadowDy 属性的值可以改变阴影与文本之间的偏移,指定android:shadowRadius属性可以产生一种文本更亮的错觉.

4.      时钟跳动的代码就是一个handler获取当前时间循环调用

private final Runnable mTimeRefresher = new Runnable() {    @Override    public void run() {        final Date d = new Date();        mTextView.setText(String.format(DATE_FORMAT, d.getHours(),                d.getMinutes(), d.getSeconds()));        mHandler.postDelayed(this, REFRESH_DELAY);    }};

5.       

显示效果

 

Hack 16 更改Toast显示的位置

Toast中的一个公共方法setGravity(int gravity, int xOffset, int yOffset);

 

Hack 24 处理空列表 

listView可以用setEmptyView(View)方法处理空状态

 

Hack 26 为LIstView添加分段表头

ListView分条目展示  并且条目头悬浮展示

http://blog.csdn.net/lu_pan_feng/article/details/51759095

 

Hack30 ListView的选择模式

ListView定义了choiceMode属性,当设置为singleChoice列表允许有一个列表项处于被选状态,如果设置为multipleChoice,那么列表项允许有任意数量的列表项处于被选状态.

可用mListView.getCheckedItemPosition()获取选中的条目

如果需要ListView处理选择行为,需要让item对应的自定义视图实现Checkable接口,并重写setChecked()方法.但是会出现点击listView的条目没有响应的问题,是因为CheckBox抢占了点击事件,需要在XML中添加下面的属性

Android:clickable=”false”

Android:focusable=”false”

Android:focusableInTouchMOde=”false”

 

 

 

0 0
原创粉丝点击