本文介绍了如何CustomView添加到布局fromXML?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要添加这个PaintingView()这是本次活动从XML文件中。
公共类全部扩展活动实现ColorPickerDialog.OnColorChangedListener {
@覆盖
保护无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.paint); 的FrameLayout myFrameLayout =(的FrameLayout)findViewById(R.id.framelayout);
MYIMAGE =(ImageView的)findViewById(R.id.ImageView01);
InputStream为= this.getResources()openRawResource(imageId1)。
originalBitmap = BitmapFactory.de codeStream(是);
myimage.setImageBitmap(originalBitmap);
myimage.setScaleType(ScaleType.MATRIX); MVIEW =(PaintingView)findViewById(R.id.my_view);
mView.setDrawingCacheEnabled(真);
mView.setVisibility(View.INVISIBLE);
最终的ImageButton漆=(的ImageButton)findViewById(R.id.paint_button);
paint.setOnClickListener(新View.OnClickListener(){
公共无效的onClick(查看视图){
mView.setVisibility(View.VISIBLE);
}
}); mPaint =新的油漆();
mPaint.setAntiAlias(真);
mPaint.setDither(真);
mPaint.setColor(0xFFFF0000地址);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(4);
运动模糊=新BlurMaskFilter(8 BlurMaskFilter.Blur.NORMAL);////////////// ********创建菜单************* ///////////// ///私有静态最终诠释COLOR_MENU_ID = Menu.FIRST;
私有静态最终诠释BLUR_MENU_ID = Menu.FIRST + 1;
私有静态最终诠释ERASE_MENU_ID = Menu.FIRST + 2;
私有静态最终诠释BRUSH_MENU_ID = Menu.FIRST + 3;@覆盖
公共布尔onCreateOptionsMenu(菜单菜单){
super.onCreateOptionsMenu(菜单);
menu.add(0,COLOR_MENU_ID,0,颜色)setShortcut('3','c')中。
menu.add(0,BLUR_MENU_ID,0,夜光抽奖)setShortcut('5','Z')。
menu.add(0,ERASE_MENU_ID,0,清除)setShortcut('5','Z')。
menu.add(0,BRUSH_MENU_ID,0,刷)setShortcut('5','Z')。
返回true;
}@覆盖
prepareOptionsMenu(菜单菜单)上公共布尔{
super.on prepareOptionsMenu(菜单);
返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项){
mPaint.setXfermode(NULL);
mPaint.setAlpha(0xFF的); 开关(item.getItemId()){
案例COLOR_MENU_ID:
新ColorPickerDialog(这个,这个,mPaint.getColor())显示()。
返回true;
案例ERASE_MENU_ID:
mPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.CLEAR));
返回true;
案例BLUR_MENU_ID:
如果(mPaint.getMaskFilter()!=运动模糊){
mPaint.setMaskFilter(运动模糊);
}其他{
mPaint.setMaskFilter(NULL);
}
返回true;
案例BRUSH_MENU_ID: sizeSeekBar.setVisibility(View.VISIBLE);
///////// ***************笔画宽度改变************* ////////// ////
sizeSeekBar.setOnSeekBarChangeListener(新SeekBar.OnSeekBarChangeListener(){
@覆盖
公共无效onProgressChanged(搜索栏搜索栏,INT进步,布尔FROMUSER){
strokeWidth = sizeSeekBar.getProgress(); // B ==进度值。
mPaint.setStrokeWidth(strokeWidth);
}
@覆盖
公共无效onStartTrackingTouch(搜索栏搜索栏){}
@覆盖
公共无效onStopTrackingTouch(搜索栏搜索栏){
上下文C = getBaseContext();
Toast.makeText(C,BrushWidth+ strokeWidth,Toast.LENGTH_SHORT).show();
sizeSeekBar.setVisibility(View.INVISIBLE);
}
});
返回true;
}
返回super.onOptionsItemSelected(项目);
}///////变色功能,从ColorPickerDialog获得价值///////
公共无效colorChanged(INT颜色){
mPaint.setColor(颜色);
}//////////// ******************* Pinting观点***************** ** //////////////////////我的绘画观/////////
公共类PaintingView扩展视图{
INT BH = originalBitmap.getHeight();
INT体重= originalBitmap.getWidth();
公共PaintingView(上下文C){
超(C); // mBitmap = Bitmap.createScaledBitmap(originalBitmap,BW,BH,真正的);
mBitmap = Bitmap.createBitmap(体重,BH,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
mBitmapPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.SRC));
}
公共PaintingView(上下文C中,int颜色){
超(C);
mBitmap = Bitmap.createBitmap(体重,BH,Bitmap.Config.ARGB_8888);
// mBitmap = Bitmap.createScaledBitmap(originalBitmap,BW,BH,真正的);
mCanvas =新的Canvas(mBitmap);
的mpath =新路径();
mBitmapPaint =新的油漆(Paint.DITHER_FLAG);
mBitmapPaint.setXfermode(新PorterDuffXfermode(PorterDuff.Mode.SRC));
mCanvas.drawColor(颜色);
}
@覆盖
保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
super.onSizeChanged(W,H,oldw,oldh);
/ * mBitmap = Bitmap.createBitmap(体重,BH,Bitmap.Config.ARGB_8888);
mCanvas =新的Canvas(mBitmap); * /
}
@覆盖
保护无效的onDraw(帆布油画){
canvas.drawColor(Color.TRANSPARENT);
canvas.drawBitmap(mBitmap,0,0,mBitmapPaint);
canvas.drawPath(的mpath,mPaint);
} //////// ************触摸evants绘画************** ///////
私人浮动MX,我的;
私有静态最终浮动TOUCH_TOLERANCE = 4;
私人无效touch_start(浮法X,浮法Y){
mPath.reset();
mPath.moveTo(X,Y);
MX = X;
我= Y;
}
私人无效TOUCH_MOVE(浮法X,浮法Y){
浮DX = Math.abs(X - MX);
浮DY = Math.abs(Y - 我的);
如果(DX> = || TOUCH_TOLERANCE DY> = TOUCH_TOLERANCE){
mPath.quadTo(MX,MY,(X + MX)/ 2,(Y + MY)/ 2);
MX = X;
我= Y;
}
}
私人无效touch_up(){
mPath.lineTo(MX,我的);
//提交的路径,我们的屏幕外
mCanvas.drawPath(的mpath,mPaint);
//杀死这个,所以我们不要双击平局
mPath.reset();
} @覆盖
公共布尔onTouchEvent(MotionEvent事件){
浮X = event.getX();
浮Y = event.getY();
开关(event.getAction()){
案例MotionEvent.ACTION_DOWN:
touch_start(X,Y);
无效();
打破; 案例MotionEvent.ACTION_MOVE:
TOUCH_MOVE(X,Y);
无效();
打破;
案例MotionEvent.ACTION_UP:
润色();
无效();
打破;
}
返回true;
} //图像触摸事件的结束} //结束MyView的
}
XML格式为:
//尝试过这样的不成功。
解决方案
<?XML版本=1.0编码=UTF-8&GT?;
<的RelativeLayout的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT机器人:layout_height =FILL_PARENT
机器人:ID =@ + ID / relativeLayout1> < com.logistics.wheresTheParty.photoTab.ImageViewTouch
机器人:ID =@ + ID / imageViewTouch的android:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT/>< / RelativeLayout的>
更改按你的包名和类名...
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.
解决方案
<?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....
这篇关于如何CustomView添加到布局fromXML?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!