梯形

自定义view

public class MyThereColorView extends ViewGroup {
    public MyThereColorView(Context context) {
        super( context );
    }

    public MyThereColorView(Context context, AttributeSet attrs) {
        super( context, attrs );
    }

    public MyThereColorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super( context, attrs, defStyleAttr );
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure( widthMeasureSpec, heightMeasureSpec );

        int totalHeight = 0;
        int totalWidth = 0;
        int childCount = getChildCount();
        if (childCount > 0) {
            for (int i = 0; i < childCount; i++) {
                View v = getChildAt( i );
                totalHeight += v.getMeasuredHeight();
                measureChild( v, widthMeasureSpec, heightMeasureSpec );
            }
        }
        totalWidth = AppUtil.screenWidth( getContext() );
        setMeasuredDimension( totalWidth, totalHeight );
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

        int l = 0;
        int t = 0;
        int r = 0;
        int b = 0;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View view = getChildAt( i );
            view.layout( l, t, l + view.getMeasuredWidth(), t + view.getMeasuredHeight() );
            l += view.getMeasuredWidth();
            t += view.getMeasuredHeight();
            if (l + view.getMeasuredWidth() > AppUtil.screenWidth( getContext() )) {
                l = 0;
            }
            final int finalI = 0;
            view.setOnClickListener( new OnClickListener() {
                @Override
                public void onClick(View v) {
                    Toast.makeText( getContext(), finalI + ":点击位置", Toast.LENGTH_SHORT ).show();
                    TextView textView = (TextView) view;
                    Toast.makeText( getContext(), textView.getText().toString(), Toast.LENGTH_SHORT ).show();
                    Intent intent = new Intent( getContext(), MainActivity.class );
                    intent.putExtra( "id", textView.getText().toString() );
                    getContext().startActivity( intent );
                }
            } );
            view.setOnLongClickListener( new OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    Toast.makeText(getContext(), finalI +":长按位置", Toast.LENGTH_SHORT).show();
                    removeView( v );
                    return true;
                }
            } );
        }
    }

}

 


activity


public class MyThereColorViewActivity extends AppCompatActivity {
    int count = 0;
    private MyThereColorView myThereColorView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_my_there_color_view );
        Button button = findViewById( R.id.bt );
        myThereColorView = findViewById( R.id.mtcv );

        button.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                  count++;
                int i = AppUtil.screenWidth( MyThereColorViewActivity.this );
                TextView textView = new TextView( MyThereColorViewActivity.this );
                textView.setText( count + "" );
                textView.setGravity( Gravity.CENTER );
                textView.setTextColor(getResources().getColor(R.color.white));

                ObjectAnimator objectAnimator = ObjectAnimator.ofFloat(textView,"translationX",(i-i/3),0);

                objectAnimator.setDuration(3000);
                objectAnimator.start();
                if (count==1||count==4||count==7||count==10||count==13||count==16){

                    textView.setBackgroundColor(getResources().getColor(R.color.colorAccent));
                }else if (count==2||count==5||count==8||count==11||count==14){
                    textView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
                }else if (count==3||count==6||count==9||count==12||count==15){
                    textView.setBackgroundColor(getResources().getColor(R.color.colory));
                }



                myThereColorView.addView(textView);

                //得到view的属性参数
                ViewGroup.LayoutParams params = textView.getLayoutParams();
                params.width = i/3;
                params.height = 70;
                textView.setLayoutParams(params);

            }
        } );
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值