如何从XML将CustomView添加到布局?

发布于 2024-12-04 01:13:59 字数 7080 浏览 1 评论 0原文

我想从 XML 文件添加此 Activity 中的 PaintingView()。

public class Full extends Activity implements  ColorPickerDialog.OnColorChangedListener  {       
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      setContentView(R.layout.paint);

      FrameLayout myFrameLayout = (FrameLayout) findViewById(R.id.framelayout);        
      myimage = (ImageView) findViewById(R.id.ImageView01);
      InputStream is = this.getResources().openRawResource(imageId1);
      originalBitmap = BitmapFactory.decodeStream(is);       
      myimage.setImageBitmap(originalBitmap);
      myimage.setScaleType(ScaleType.MATRIX);  

      mView = (PaintingView)findViewById(R.id.my_view);
      mView.setDrawingCacheEnabled(true);
      mView.setVisibility(View.INVISIBLE);
      final ImageButton paint = (ImageButton) findViewById(R.id.paint_button); 
         paint.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            mView.setVisibility(View.VISIBLE);
         }
      });

      mPaint = new Paint();
      mPaint.setAntiAlias(true);
      mPaint.setDither(true);
      mPaint.setColor(0xFFFF0000);
      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeJoin(Paint.Join.ROUND);
      mPaint.setStrokeCap(Paint.Cap.ROUND);
      mPaint.setStrokeWidth(4);
      mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

//////////////******** Menu Creation *************////////////////

private static final int COLOR_MENU_ID = Menu.FIRST;
private static final int BLUR_MENU_ID = Menu.FIRST + 1;
private static final int ERASE_MENU_ID = Menu.FIRST + 2;
private static final int BRUSH_MENU_ID = Menu.FIRST + 3;

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
    menu.add(0, BLUR_MENU_ID, 0, "Glow Draw").setShortcut('5', 'z');
    menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
    menu.add(0, BRUSH_MENU_ID, 0, "Brush").setShortcut('5', 'z');
    return true;
}

@Override 
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
       return true;
}

@Override 
public boolean onOptionsItemSelected(MenuItem item) {
    mPaint.setXfermode(null);
    mPaint.setAlpha(0xFF);

    switch (item.getItemId()) {
        case COLOR_MENU_ID:
            new ColorPickerDialog(this,this, mPaint.getColor()).show();
            return true;
        case ERASE_MENU_ID:
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            return true;
        case BLUR_MENU_ID:
            if (mPaint.getMaskFilter() != mBlur) {
                mPaint.setMaskFilter(mBlur);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case BRUSH_MENU_ID:

            sizeSeekBar.setVisibility(View.VISIBLE);
          /////////*************** Stroke width changed *************//////////////
         sizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
         @Override
         public void onProgressChanged(SeekBar seekBar,int progress, boolean fromUser) {
             strokeWidth = sizeSeekBar.getProgress();   // b == progress value.     
             mPaint.setStrokeWidth(strokeWidth);    
         }
         @Override
         public void onStartTrackingTouch(SeekBar seekBar) {    }
         @Override
         public void onStopTrackingTouch(SeekBar seekBar) {
             Context c = getBaseContext();
             Toast.makeText(c, " BrushWidth " + strokeWidth, Toast.LENGTH_SHORT).show();
             sizeSeekBar.setVisibility(View.INVISIBLE);
         }
         });
         return true;  
    }
    return super.onOptionsItemSelected(item);
}

///////  colour changed function, getting value from ColorPickerDialog   ///////
public void colorChanged(int color) {
    mPaint.setColor(color);
}

////////////******************* Pinting view *******************///////////////////

/// MY PAINTING view /////////
public class PaintingView extends View {
    int bh = originalBitmap.getHeight();
    int bw = originalBitmap.getWidth();
    public PaintingView(Context c)  {
        super(c);

        //mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }
    public PaintingView (Context c, int color)  {  
        super(c);
        mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        //mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;
        mCanvas.drawColor(color);
    } 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);           
            /*mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);*/
    }
    @Override 
    protected void onDraw(Canvas canvas) {   
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

    ////////************touching evants for painting**************///////
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }  //end of touch events for image

}// end MyView  

Xml

格式为: // 像这样尝试不成功。

I want to add this PaintingView() that is in this Activity from XML file.

public class Full extends Activity implements  ColorPickerDialog.OnColorChangedListener  {       
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
      setContentView(R.layout.paint);

      FrameLayout myFrameLayout = (FrameLayout) findViewById(R.id.framelayout);        
      myimage = (ImageView) findViewById(R.id.ImageView01);
      InputStream is = this.getResources().openRawResource(imageId1);
      originalBitmap = BitmapFactory.decodeStream(is);       
      myimage.setImageBitmap(originalBitmap);
      myimage.setScaleType(ScaleType.MATRIX);  

      mView = (PaintingView)findViewById(R.id.my_view);
      mView.setDrawingCacheEnabled(true);
      mView.setVisibility(View.INVISIBLE);
      final ImageButton paint = (ImageButton) findViewById(R.id.paint_button); 
         paint.setOnClickListener(new View.OnClickListener() {
         public void onClick(View view) {
            mView.setVisibility(View.VISIBLE);
         }
      });

      mPaint = new Paint();
      mPaint.setAntiAlias(true);
      mPaint.setDither(true);
      mPaint.setColor(0xFFFF0000);
      mPaint.setStyle(Paint.Style.STROKE);
      mPaint.setStrokeJoin(Paint.Join.ROUND);
      mPaint.setStrokeCap(Paint.Cap.ROUND);
      mPaint.setStrokeWidth(4);
      mBlur = new BlurMaskFilter(8, BlurMaskFilter.Blur.NORMAL);

//////////////******** Menu Creation *************////////////////

private static final int COLOR_MENU_ID = Menu.FIRST;
private static final int BLUR_MENU_ID = Menu.FIRST + 1;
private static final int ERASE_MENU_ID = Menu.FIRST + 2;
private static final int BRUSH_MENU_ID = Menu.FIRST + 3;

@Override 
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);
    menu.add(0, COLOR_MENU_ID, 0, "Color").setShortcut('3', 'c');
    menu.add(0, BLUR_MENU_ID, 0, "Glow Draw").setShortcut('5', 'z');
    menu.add(0, ERASE_MENU_ID, 0, "Erase").setShortcut('5', 'z');
    menu.add(0, BRUSH_MENU_ID, 0, "Brush").setShortcut('5', 'z');
    return true;
}

@Override 
public boolean onPrepareOptionsMenu(Menu menu) {
    super.onPrepareOptionsMenu(menu);
       return true;
}

@Override 
public boolean onOptionsItemSelected(MenuItem item) {
    mPaint.setXfermode(null);
    mPaint.setAlpha(0xFF);

    switch (item.getItemId()) {
        case COLOR_MENU_ID:
            new ColorPickerDialog(this,this, mPaint.getColor()).show();
            return true;
        case ERASE_MENU_ID:
            mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.CLEAR));
            return true;
        case BLUR_MENU_ID:
            if (mPaint.getMaskFilter() != mBlur) {
                mPaint.setMaskFilter(mBlur);
            } else {
                mPaint.setMaskFilter(null);
            }
            return true;
        case BRUSH_MENU_ID:

            sizeSeekBar.setVisibility(View.VISIBLE);
          /////////*************** Stroke width changed *************//////////////
         sizeSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener(){
         @Override
         public void onProgressChanged(SeekBar seekBar,int progress, boolean fromUser) {
             strokeWidth = sizeSeekBar.getProgress();   // b == progress value.     
             mPaint.setStrokeWidth(strokeWidth);    
         }
         @Override
         public void onStartTrackingTouch(SeekBar seekBar) {    }
         @Override
         public void onStopTrackingTouch(SeekBar seekBar) {
             Context c = getBaseContext();
             Toast.makeText(c, " BrushWidth " + strokeWidth, Toast.LENGTH_SHORT).show();
             sizeSeekBar.setVisibility(View.INVISIBLE);
         }
         });
         return true;  
    }
    return super.onOptionsItemSelected(item);
}

///////  colour changed function, getting value from ColorPickerDialog   ///////
public void colorChanged(int color) {
    mPaint.setColor(color);
}

////////////******************* Pinting view *******************///////////////////

/// MY PAINTING view /////////
public class PaintingView extends View {
    int bh = originalBitmap.getHeight();
    int bw = originalBitmap.getWidth();
    public PaintingView(Context c)  {
        super(c);

        //mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
    }
    public PaintingView (Context c, int color)  {  
        super(c);
        mBitmap = Bitmap.createBitmap(bw,bh,Bitmap.Config.ARGB_8888);
        //mBitmap = Bitmap.createScaledBitmap(originalBitmap,bw,bh,true);
        mCanvas = new Canvas(mBitmap);
        mPath = new Path();
        mBitmapPaint = new Paint(Paint.DITHER_FLAG);
        mBitmapPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC)) ;
        mCanvas.drawColor(color);
    } 
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);           
            /*mBitmap = Bitmap.createBitmap(bw, bh, Bitmap.Config.ARGB_8888);
            mCanvas = new Canvas(mBitmap);*/
    }
    @Override 
    protected void onDraw(Canvas canvas) {   
        canvas.drawColor(Color.TRANSPARENT);
        canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
        canvas.drawPath(mPath, mPaint);
    }

    ////////************touching evants for painting**************///////
    private float mX, mY;
    private static final float TOUCH_TOLERANCE = 4;
    private void touch_start(float x, float y) {
        mPath.reset();
        mPath.moveTo(x, y);
        mX = x;
        mY = y;
    }
    private void touch_move(float x, float y) {
        float dx = Math.abs(x - mX);
        float dy = Math.abs(y - mY);
        if (dx >= TOUCH_TOLERANCE || dy >= TOUCH_TOLERANCE) {
            mPath.quadTo(mX, mY, (x + mX)/2, (y + mY)/2);
            mX = x;
            mY = y;
        }
    }
    private void touch_up() {
        mPath.lineTo(mX, mY);
        // commit the path to our offscreen
        mCanvas.drawPath(mPath, mPaint);
        // kill this so we don't double draw
        mPath.reset();
    }

    @Override 
    public boolean onTouchEvent(MotionEvent event) {
        float x = event.getX();
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touch_start(x, y);
                invalidate();
                break;

            case MotionEvent.ACTION_MOVE:
                touch_move(x, y);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                touch_up();
                invalidate();
                break;
        }
        return true;
    }  //end of touch events for image

}// end MyView  

}

Xml format is:

// tried like this UnSuccess.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

屋顶上的小猫咪 2024-12-11 01:13:59
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/relativeLayout1">

    <com.logistics.wheresTheParty.photoTab.ImageViewTouch
        android:id="@+id/imageViewTouch" android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</RelativeLayout>

根据您的包名称和类名进行更改......

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent"
    android:id="@+id/relativeLayout1">

    <com.logistics.wheresTheParty.photoTab.ImageViewTouch
        android:id="@+id/imageViewTouch" android:layout_width="fill_parent"
        android:layout_height="fill_parent"/>

</RelativeLayout>

Change as per your package name and classname....

你げ笑在眉眼 2024-12-11 01:13:59

将自定义视图添加到您的 xml 布局中。

<ParentLayout>
<yourPackagePath.customviewname
//layout attributes come here
/>
</ParentLayout>

您可以使用在您的问题的特定情况下

<pathtoyourclass.PaintingView
//layout attributes
/>

you can add your custom view to your xml layout using

<ParentLayout>
<yourPackagePath.customviewname
//layout attributes come here
/>
</ParentLayout>

In the particular case of your question.

<pathtoyourclass.PaintingView
//layout attributes
/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文