如何通过OnDraw的前变量的自定义视图()被调用? [英] How to pass variables to custom View before onDraw() is called?

查看:119
本文介绍了如何通过OnDraw的前变量的自定义视图()被调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我所试图实现的:

  • 在衡量我的布局,mainContainer上一个容器查看,即在 XML定义
  • 通过 mainContainer上的宽度和高度来一个不同的自定义查看的OnDraw()
  • 我要传递的宽度和高度,以便定制查看知道在哪里画 canvas.drawBitmap 使用坐标
  • 在自定义视图将编程从code创建

如何通过测量INT宽度和INT高度的OnDraw()之前,我自定义的视图被称为?

自定义视图

 公共类AvatarView扩展ImageView的{

私人位图的身体;
私人位图的帽子;

公共AvatarView(上下文的背景下){
    超(上下文);
    在里面();
}

公共AvatarView(上下文的背景下,ATTRS的AttributeSet){
    超(背景下,ATTRS);
    在里面();
}

公共AvatarView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
    超(背景下,ATTRS,defStyle);
    在里面();
}

私人无效的init(){
    身体= BitmapFactory.de codeResource(getResources(),R.drawable.battle_run_char);
    帽子= BitmapFactory.de codeResource(getResources(),R.drawable.red_cartoon_hat);
}

@覆盖
保护无效的OnDraw(帆布油画){
    super.onDraw(画布);
    canvas.drawBitmap(体,X,Y,空);
    canvas.drawBitmap(帽子,X,Y,空);
}
}
 

片段

  @覆盖
公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,捆绑savedInstanceState){
    查看查看= inflater.inflate(R.layout.fragment_customize_avatar,集装箱,假);
    最后RelativeLayout的mainContainer上=(RelativeLayout的)view.findViewById(R.id.main_container);
    TwoWayView inventoryList =(TwoWayView)view.findViewById(R.id.inventory);

    inventoryList.setAdapter(空);

    inventoryList.setOnItemClickListener(新OnItemClickListener(){
        @覆盖
        公共无效onItemClick(适配器视图<>为arg0,查看ARG1,INT ARG2,长ARG3){

        }
    });

    mainContainer.getViewTreeObserver()。addOnGlobalLayoutListener(新OnGlobalLayoutListener(){
        @燮pressLint(NewApi)
        @燮pressWarnings(德precation)
        @覆盖
        公共无效onGlobalLayout(){
            //获得的宽度和高度
            containerWidth = mainContainer.getWidth();
            containerHeight = mainContainer.getHeight();

            //删除全局监听器
            如果(android.os.Build.VERSION.SDK_INT> = android.os.Build.VERSION_ codeS.JELLY_BEAN)
                。mainContainer.getViewTreeObserver()removeOnGlobalLayoutListener(本);
            其他
                。mainContainer.getViewTreeObserver()removeGlobalOnLayoutListener(本);
        }
    });

    返回查看;
}
 

XML

 <的LinearLayout
的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=垂直
机器人:可点击=真
机器人:后台=#FFF>

< com.walintukai.lfdate.CustomTextView
    机器人:layout_width =match_parent
    机器人:layout_height =WRAP_CONTENT
    机器人:重力=中心
    机器人:paddingTop =10dp
    机器人:paddingBottom会=10dp
    机器人:TEXTSTYLE =黑体
    机器人:文字颜色=#FFF
    机器人:TEXTSIZE =20SP
    机器人:textAllCaps =真
    机器人:文本=@字符串/ customize_avatar
    机器人:后台=#009BFF/>

< RelativeLayout的
    机器人:ID =@ + ID / main_container
    机器人:layout_width =match_parent
    机器人:layout_height =0dp
    机器人:layout_weight =1/>

< org.lucasr.twowayview.TwoWayView
    的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    的xmlns:程序=htt​​p://schemas.android.com/apk/res-auto
    机器人:ID =@ + ID /库存
    风格=@风格/ Horizo​​ntalListView
    机器人:layout_width =match_parent
    机器人:layout_height =80dp
    机器人:drawSelectorOnTop =假
    机器人:后台=#f3f3f3/>

< / LinearLayout中>
 

解决方案

您需要做的是增加内部的标志你的 AvatarView 的检查,如果你打算渲染这还是不是你的的OnDraw 方法。

示例:

 公共类AvatarView扩展ImageView的{

    私人位图的身体;
    私人位图的帽子;
    私人诠释containerHeight;
    私人诠释containerWidth;
    私人布尔isRender = FALSE;

    公共AvatarView(上下文的背景下){
        超(上下文);
        在里面();
    }

    公共AvatarView(上下文的背景下,ATTRS的AttributeSet){
        超(背景下,ATTRS);
        在里面();
    }

    公共AvatarView(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
        超(背景下,ATTRS,defStyle);
        在里面();
    }

    公共无效setMeasure(INT containerWidth,INT containerHeight)
    {
        this.containerHeight = containerHeight;
        this.containerWidth = containerWidth;
    }

    私人无效的init(){
        身体= BitmapFactory.de codeResource(getResources(),R.drawable.battle_run_char);
        帽子= BitmapFactory.de codeResource(getResources(),R.drawable.red_cartoon_hat);
    }

    公共无效setRender(布尔渲染)
    {
       isRender =渲染;
    }

    @覆盖
    保护无效的OnDraw(帆布油画){
        super.onDraw(画布);
        如果(isRender)
        {
            canvas.drawBitmap(体,X,Y,空);
            canvas.drawBitmap(帽子,X,Y,空);
        }

    }
    }
 

现在它时,你不叫 setRender ,并将其设置为不会呈现真正。并调用 setMeasure 传递的价值。

首先,您需要调用 setMeasure ,并在设置后你再调用measure setRender(真)和呼叫无效()致电的OnDraw 方法来渲染图像

What I am trying to achieve:

  • measure a container View in my layout, mainContainer, that is defined in the XML
  • pass the mainContainer's width and height to a different custom View before onDraw() is called
  • I want to pass the width and height so the custom View knows where to draw canvas.drawBitmap using coordinates
  • The custom view will be programmatically created from code

How can I pass the measured int width and int height to my custom View before onDraw() is called?

Custom View

public class AvatarView extends ImageView {

private Bitmap body;
private Bitmap hat;

public AvatarView(Context context) {
    super(context);
    init();
}

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

public AvatarView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    init();
}

private void init() {
    body = BitmapFactory.decodeResource(getResources(), R.drawable.battle_run_char);
    hat = BitmapFactory.decodeResource(getResources(), R.drawable.red_cartoon_hat);
}

@Override
protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    canvas.drawBitmap(body, x, y, null);
    canvas.drawBitmap(hat, x, y, null);
}
}

Fragment

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_customize_avatar, container, false);
    final RelativeLayout mainContainer = (RelativeLayout) view.findViewById(R.id.main_container);
    TwoWayView inventoryList = (TwoWayView) view.findViewById(R.id.inventory);

    inventoryList.setAdapter(null);

    inventoryList.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

        }
    });

    mainContainer.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @SuppressLint("NewApi")
        @SuppressWarnings("deprecation")
        @Override
        public void onGlobalLayout() {
            // Retrieve the width and height
            containerWidth = mainContainer.getWidth();
            containerHeight = mainContainer.getHeight();

            // Remove global listener
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN)
                mainContainer.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            else
                mainContainer.getViewTreeObserver().removeGlobalOnLayoutListener(this);
        }
    });

    return view;
}

XML

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:clickable="true"
android:background="#fff" >

<com.walintukai.lfdate.CustomTextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:paddingTop="10dp"
    android:paddingBottom="10dp"
    android:textStyle="bold"
    android:textColor="#fff"
    android:textSize="20sp"
    android:textAllCaps="true"
    android:text="@string/customize_avatar"
    android:background="#009BFF" />

<RelativeLayout
    android:id="@+id/main_container"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1" />

<org.lucasr.twowayview.TwoWayView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/inventory"
    style="@style/HorizontalListView"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:drawSelectorOnTop="false"
    android:background="#f3f3f3" />

</LinearLayout>

解决方案

What you need to do is add a flag inside your AvatarView that checks if are you going to render this or not in your onDraw method.

sample:

   public class AvatarView extends ImageView {

    private Bitmap body;
    private Bitmap hat;
    private int containerHeight;
    private int containerWidth;
    private boolean isRender = false;

    public AvatarView(Context context) {
        super(context);
        init();
    }

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

    public AvatarView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init();
    }

    public void setMeasure(int containerWidth, int containerHeight )
    {
        this.containerHeight = containerHeight;
        this.containerWidth = containerWidth;
    }

    private void init() {
        body = BitmapFactory.decodeResource(getResources(), R.drawable.battle_run_char);
        hat = BitmapFactory.decodeResource(getResources(), R.drawable.red_cartoon_hat);
    }

    public void setRender(boolean render)
    {
       isRender = render;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(isRender )
        {
            canvas.drawBitmap(body, x, y, null);
            canvas.drawBitmap(hat, x, y, null);
        }

    }
    }

Now it wont render when you dont call setRender and set it to true. And just call setMeasure to pass the value.

First you need to call setMeasure and after you set the measure you then call setRender(true) and call invalidate() to call the onDraw method to render the images

这篇关于如何通过OnDraw的前变量的自定义视图()被调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆