使用Canvas或使用CustomDrawableView类android的2个或更多矩形

时间:2016-04-22 14:11:42

标签: android android-canvas

我想在同一Canvas中使用更多矩形,因此我将它们展示在同一activity中。 但是当我添加第二个CustomDrawableView我的活动时,使用特殊类(SetContentView(my_second_Drawable))是全白的。 相反,使用my_canvas.drawRect(x,y,width,height,my_paint)无矩形。

我搜索网络但我找不到任何东西。 你能帮我吗? 抱歉我的英文。



package com.example.prova.shaperectprove;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.v4.app.FragmentManager;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.TransitionDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private LinearLayout myLayout,redLayout,greenLayout;

    ShapeDrawable myshape;
    CustomDrawableView greenDrawableView;
    CustomDrawableView redDrawableView;
    CustomDrawableView blueDrawableView;
    Canvas mycanvas = new Canvas();

    final private static String str = "Green";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);

        myLayout = new LinearLayout(getApplicationContext());
        myLayout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 50));
        myLayout.setOrientation(LinearLayout.HORIZONTAL);

        LinearLayout.LayoutParams myLLP = new LinearLayout.LayoutParams(200, 50);
        LinearLayout.LayoutParams myLLP2 = new LinearLayout.LayoutParams(200, 50);


        redLayout = new LinearLayout(getApplicationContext());
        redLayout.setLayoutParams(myLLP2);
        redLayout.setOrientation(LinearLayout.HORIZONTAL);

        greenLayout = new LinearLayout(getApplicationContext());
        greenLayout.setLayoutParams(myLLP);
        greenLayout.setOrientation(LinearLayout.HORIZONTAL);
        
        //dichiarazione
        greenDrawableView = new CustomDrawableView(this);
        redDrawableView = new CustomDrawableView(this);
        blueDrawableView = new CustomDrawableView(this);

        if (greenDrawableView.isClickable()) {
            Log.i("Cliccabile", "TRUE_clickable");
        }
        greenDrawableView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                FragmentManager fm = getSupportFragmentManager();
                MyDialogGreen dialogGreen = new MyDialogGreen();
                dialogGreen.show(fm, str);
            }
        });

       
        greenDrawableView.changeSize(10,10, 210, 60); //left,top,right,bottom
        redDrawableView.changeSize(210, 10, 420, 60);
        redDrawableView.changeColor(0xFFC12727);

        greenLayout.addView(greenDrawableView);
        redLayout.addView(redDrawableView);

        myLayout.addView(greenLayout);
        myLayout.addView(redLayout);
        setContentView(myLayout);
        }
}






package com.example.prova.shaperectprove;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.drawable.ShapeDrawable;
import android.graphics.drawable.shapes.RectShape;
import android.text.Layout;
import android.text.style.RelativeSizeSpan;
import android.view.View;
import android.widget.RelativeLayout;


public class CustomDrawableView extends View {

    private ShapeDrawable mDrawable;
    int x,y,width,height,drawColor = 0xFF1A9316;

    public CustomDrawableView(Context context) {
        super(context);


        x = 20;
        y = 20;
        width = 200;
        height = 60;

        mDrawable = new ShapeDrawable(new RectShape());
        mDrawable.getPaint().setColor(drawColor);

        mDrawable.setBounds(x, y, width, height); /
    }

    protected void onDraw(Canvas canvas) {
        
        mDrawable.draw(canvas);


    }

    protected void changeColor(int newColor) {

        drawColor = newColor;
    }

    protected int getLenght()
    {
        return width-x; //right-left
    }

    protected int getHight()
    {
        return height-y; //bottom-top
    }

    protected int mygetLeft()
    {
        return x;
    }
    protected int mygetTop()
    {
        return y;
    }

    protected int mygetRight()
    {
        return width;
    }

    protected int mygetBottom()
    {
        return height;
    }


    protected void changeSize(int x, int y,int width, int height){
        mDrawable.setBounds(x, y, width, height); 
        invalidate();
    }


}




0 个答案:

没有答案