11-8

来源:互联网 发布:js array indexof ie5 编辑:程序博客网 时间:2024/05/01 15:43

1 刷新效果


package com.example.myapplication;import android.content.Context;import android.graphics.drawable.AnimationDrawable;import android.os.Handler;import android.util.AttributeSet;import android.util.Log;import android.view.View;import android.view.ViewGroup;import android.widget.ImageView;import android.widget.LinearLayout;import android.widget.TextView;import com.aspsine.swipetoloadlayout.SwipeRefreshTrigger;import com.aspsine.swipetoloadlayout.SwipeTrigger;/** * Created by tanyl on 2016/10/21 16:28 */public class CustomRefreshHeadView extends LinearLayout implements SwipeTrigger, SwipeRefreshTrigger {    TextView tvStatus;    ImageView imageAnim;    AnimationDrawable animationDrawable;    public CustomRefreshHeadView(Context context) {        this(context, null, 0);    }    public CustomRefreshHeadView(Context context, AttributeSet attrs) {        this(context, attrs, 0);    }    public CustomRefreshHeadView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        init();    }    private void init() {        //这个view随意定义        //这里的原理就是简单的动态布局添加        ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);        View view = View.inflate(getContext(), R.layout.buju, null);        addView(view, lp);        tvStatus = (TextView) view.findViewById(R.id.shuaxin);        imageAnim = (ImageView) view.findViewById(R.id.imageanim);        animationDrawable = (AnimationDrawable) getResources().getDrawable(R.drawable.anim);        imageAnim.setBackgroundDrawable(animationDrawable);    }    @Override    public void onRefresh() {        tvStatus.setText("正在拼命加载数据...");            start();        Log.i("onRefresh", animationDrawable.isRunning()+"");    }    @Override    public void onPrepare() {    }    @Override    public void onMove(int y, boolean isComplete, boolean automatic) {        if (!isComplete) {            if (y > getHeight()) {                tvStatus.setText("释放刷新");            } else {                tvStatus.setText("刷新");            }        } else {            tvStatus.setText("刷新返回");        }    }    @Override    public void onRelease() {    }    @Override    public void onComplete() {        tvStatus.setText("刷新完成");        stop();        Log.i("onComplete", animationDrawable.isRunning()+"");    }    @Override    public void onReset() {        tvStatus.setText("");    }    protected void start() {        if (animationDrawable != null && !animationDrawable.isRunning()) {         animationDrawable.start();        }    }    protected void stop() {        if (animationDrawable != null && animationDrawable.isRunning()) {         animationDrawable.stop();        }    }}


public class MainActivity extends AppCompatActivity implements OnRefreshListener  {    CustomRefreshHeadView refreshHeadView;    public  SwipeToLoadLayout swipeToLoadLayout;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        swipeToLoadLayout= (SwipeToLoadLayout) findViewById(R.id.swipeToLoad);        refreshHeadView= (CustomRefreshHeadView) findViewById(R.id.swipe_refresh_header);        swipeToLoadLayout.setRefreshHeaderView(refreshHeadView);        swipeToLoadLayout.setOnRefreshListener(this);    }    @Override    public void onRefresh() {        swipeToLoadLayout.postDelayed(new Runnable() {            @Override            public void run() {                swipeToLoadLayout.setRefreshing(false);            }        }, 3000);    }

2 自定义View ,onMeasure方法

@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {    int desiredWidth = 100;    int desiredHeight = 100;    int widthMode = MeasureSpec.getMode(widthMeasureSpec);    int widthSize = MeasureSpec.getSize(widthMeasureSpec);    int heightMode = MeasureSpec.getMode(heightMeasureSpec);    int heightSize = MeasureSpec.getSize(heightMeasureSpec);    int width;    int height;    //测量宽度    if (widthMode == MeasureSpec.EXACTLY) {        width = widthSize;    } else if (widthMode == MeasureSpec.AT_MOST) {        width = Math.min(desiredHeight, widthSize);    } else {        width = desiredWidth;    }    //测量高度    if (heightMode == MeasureSpec.EXACTLY) {        height = heightSize;    } else if (heightMode == MeasureSpec.AT_MOST) {        height = Math.min(desiredHeight, heightSize);    } else {        height = desiredHeight;    }    setMeasuredDimension(width, height);}

RectF rectF=new RectF(left,top,right,buttom);

其实矩形的左上角坐标(left,top);右下角坐标(right,buttom);


3 文字换行

int textWidth = (int) textPaint.measureText(text.substring(0, 2));//超过textWidth宽度就准备换行,staticLayout = new StaticLayout(text, textPaint, textWidth, Layout.Alignment.ALIGN_NORMAL, 1F, 0, false);

canvas.translate(width / 2, height / 2 - staticLayout.getHeight() / 2);staticLayout.draw(canvas);

需要指出的是这个layout是默认画在Canvas的(0,0)点的,如果需要调整位置只能在draw之前移Canvas的起始坐标
canvas.translate(x,y);

4 图文混排


<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"              android:orientation="vertical"              android:layout_width="match_parent"              android:layout_height="match_parent">        <com.zhy.view.MixtureTextView        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/id_mixtureTextview"        android:padding="16dp"        android:text="@string/text1"        android:textColor="#ff5d75"        android:textSize="14sp"        >        <ImageView            android:layout_width="wrap_content"            android:layout_alignParentRight="true"            android:src="@drawable/icon"            android:layout_height="wrap_content"/>        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:src="@drawable/android2"            />        <ImageView            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:layout_marginLeft="100dp"            android:layout_marginTop="100dp"            android:src="@drawable/icon"            />    </com.zhy.view.MixtureTextView></RelativeLayout>





0 0
原创粉丝点击