android自定义View实现裁剪图片功能,不使用系统的
2012-08-23 17:09
1101 查看
闲来无事,今天写了一下怎么裁剪图片,写的很烂,放在这里以后看看自己有没有进步.............
首先给个效果图了...........
1,拖动矩形框,红色矩形框以外是一个半透明的罩,通过画出四个半透明的矩形,可以实现效果
2,拖动红框及拉伸红框获取坐标,拉伸只能通过红框的上面那个红线点击向外移动放大,向左里移动缩小,哈哈,还有很多地方没有做,不想做了,懒啊,有想法的时候再做吧.......
3,点击红框以外的地方时候,会保存裁剪图片,最后点击save去掉自定义的view,把裁剪的图片放在imageview里面。(底层有个图片是本身imageview有的哦)
代码实现:
自定义view:
下面是mainactivity类使用myview及监听事件:
xml布局文件:
还有很多的地方不足...............如果有哪位该人能够给个简单的方法就好啊......,代码很看,可不要喷饭哦............
quot;,
首先给个效果图了...........
1,拖动矩形框,红色矩形框以外是一个半透明的罩,通过画出四个半透明的矩形,可以实现效果
2,拖动红框及拉伸红框获取坐标,拉伸只能通过红框的上面那个红线点击向外移动放大,向左里移动缩小,哈哈,还有很多地方没有做,不想做了,懒啊,有想法的时候再做吧.......
3,点击红框以外的地方时候,会保存裁剪图片,最后点击save去掉自定义的view,把裁剪的图片放在imageview里面。(底层有个图片是本身imageview有的哦)
代码实现:
自定义view:
package com.zte.caijian; import android.annotation.SuppressLint; import android.content.Context; import android.graphics.Bitmap; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Paint.Style; import android.util.Log; import android.view.MotionEvent; import android.view.View; public class myView extends View { private int l = 20; private int t = 20; private int r = 100; private int b = 100; Bitmap bitmap; Bitmap bitmapOld; @SuppressLint({ "NewApi", "NewApi" }) public myView(Context context, Bitmap bitmap) { super(context); this.bitmap = Bitmap.createScaledBitmap(bitmap, 480, 540, true); // setBackgroundColor(Color.BLACK); // setAlpha(0.8f); } int xy[] = new int[2]; int temp[] = new int[2]; int tempM[] = new int[2]; boolean mFlag = false; boolean mFlagM = false; boolean mFlag1 = false; boolean mFlag2 = false; boolean kaishi = true; int change = 0; @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: xy[0] = (int) event.getX(); xy[1] = (int) event.getY(); if (xy[0] < r && xy[0] > l && xy[1] < b && xy[1] > t) { mFlag2 = false; mFlag = true; mFlag1 = false; } else { mFlag = false; } if (xy[0] < r && xy[0] > l && xy[1] == t) { mFlag1 = true; } else { if (tmL == 0 || tmR == 0 || tmT == 0) { } else { if (bitmapOld == null) { Log.i("RG", "bitmap--->>>h" + bitmap.getHeight()); Log.i("RG", "bitmap--->>>W" + bitmap.getWidth()); Log.i("RG", "tmL--->>>W" + tmL); Log.i("RG", "tmT--->>>W" + tmT); Log.i("RG", "tmR--->>>W" + tmR); Log.i("RG", "tmB--->>>W" + tmB); bitmapOld = Bitmap.createBitmap(bitmap, tmL, tmT, tmR - tmL, tmB - tmT, null, true); } else { bitmapOld = null; bitmapOld = Bitmap.createBitmap(bitmap, tmL, tmT, tmR - tmL, tmB - tmT, null, true); } } } case MotionEvent.ACTION_MOVE: temp[0] = (int) event.getX(); temp[1] = (int) event.getY(); if (mFlag) { if (xy[0] == temp[0] && xy[1] == temp[1]) { } else { mFlagM = true; l = temp[0]; t = temp[1]; if (tuodongH > 0) { r = l + tuodongW; b = t + tuodongH; } else { r = l + 80; b = t + 80; } invalidate(); } } if (mFlag1) { Log.i("RG", "panduanl you wentil ---->>>" + xy[0] + ",,,," + xy[1]); if (temp[0] > xy[0] || temp[1] < xy[1]) { Log.i("RG", "-------mFlag1------>>>>>>" + temp[0] + ",,,," + temp[1]); mFlag2 = true; change += 5; // l = xy[0]; // t = xy[1]; tempM[0] = r = (l + 80) + change; tempM[1] = b = (t + 80) + change; invalidate(); } else { change -= 5; tempM[0] = r = (l + 80) + change; tempM[1] = b = (t + 80) + change; invalidate(); } } } return true; } int tuodongH; int tuodongW; int tmL; int tmT; int tmR; int tmB; public Bitmap getBitmap() { return bitmapOld; } public void Pcanvas(Canvas canvas) { Paint mPaint = new Paint(); mPaint.setStyle(Style.STROKE); mPaint.setStrokeMiter(6); mPaint.setStrokeWidth(3); mPaint.setColor(Color.RED); canvas.drawRect(l, t, r, b, mPaint); tuodongH = b - t; tuodongW = r - l; Paint mPaint1 = new Paint(); mPaint1.setColor(Color.BLACK); mPaint1.setAlpha(100); canvas.drawRect(0, 0, 480, t, mPaint1);// 1111 canvas.drawRect(0, b, 480, 800, mPaint1); canvas.drawRect(0, t, l, b, mPaint1); canvas.drawRect(r, t, 480, b, mPaint1); } @Override protected void onDraw(Canvas canvas) { if (mFlag1 && mFlag2) { Pcanvas(canvas); // mFlag1 = false; // mFlag2 = false; return; } if (mFlag == true && mFlagM == true) { Paint mPaint = new Paint(); mPaint.setStyle(Style.STROKE); mPaint.setStrokeMiter(6); mPaint.setStrokeWidth(3); mPaint.setColor(Color.RED); canvas.drawRect(l, t, r, b, mPaint); tmL = l; tmT = t; tmR = r; tmB = b; Paint mPaint1 = new Paint(); mPaint1.setColor(Color.BLACK); mPaint1.setAlpha(100); canvas.drawRect(0, 0, 480, t, mPaint1);// 1111 canvas.drawRect(0, b, 480, 800, mPaint1); canvas.drawRect(0, t, l, b, mPaint1); canvas.drawRect(r, t, 480, b, mPaint1); return; } if (kaishi) { Paint mPaint = new Paint(); mPaint.setStyle(Style.STROKE); mPaint.setStrokeMiter(6); mPaint.setStrokeWidth(3); mPaint.setColor(Color.RED); canvas.drawRect(l, t, r, b, mPaint); Paint mPaint1 = new Paint(); mPaint1.setColor(Color.BLACK); mPaint1.setAlpha(100); canvas.drawRect(0, 0, 480, t, mPaint1);// 1111 canvas.drawRect(0, b, 480, 800, mPaint1); canvas.drawRect(0, t, l, b, mPaint1); canvas.drawRect(r, t, 480, b, mPaint1); kaishi = false; } else { return; } super.onDraw(canvas); } }
下面是mainactivity类使用myview及监听事件:
package com.zte.caijian; import android.os.Bundle; import android.annotation.SuppressLint; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.support.v4.app.NavUtils; public class MainActivity extends Activity implements OnClickListener { Button save; Button cencal; ImageView mImageView; ImageView mCaijian; myView my; Bitmap bitmap; LayoutParams lp = new LayoutParams(480, 600); @SuppressLint("NewApi") @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); init(); bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.aa); my = new myView(MainActivity.this, bitmap); LinearLayout layout = new LinearLayout(MainActivity.this); layout.addView(my); layout.setTop(50); this.addContentView(layout, lp); } private void init() { save = (Button) findViewById(R.id.save); cencal = (Button) findViewById(R.id.cencal); mImageView = (ImageView) findViewById(R.id.imageview); mCaijian = (ImageView) findViewById(R.id.caijian); save.setOnClickListener(this); cencal.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.save: Log.i("RG", "CLICK-----"); Bitmap bitmap = my.getBitmap(); mImageView.setImageBitmap(bitmap); break; case R.id.cencal: MainActivity.this.finish(); break; } } }
xml布局文件:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageview" android:layout_width="480dip" android:layout_height="360dip" android:background="@drawable/aa" /> <Button android:id="@+id/save" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="400dip" android:text="save" /> <Button android:id="@+id/cencal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_marginTop="400dip" android:text="cencal" /> <ImageView android:id="@+id/caijian" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="400dip" android:layout_centerHorizontal="true" /> </RelativeLayout>
还有很多的地方不足...............如果有哪位该人能够给个简单的方法就好啊......,代码很看,可不要喷饭哦............
quot;,
相关文章推荐
- Android圆形图片不求人,自定义View实现(BitmapShader使用)
- 使用Java代码在Android中实现图片裁剪功能
- ImageView的使用,实现本地图片的适屏显示和裁剪功能。
- Android使用BitmapShader图形渲染实现圆形、圆角和椭圆自定义图片View
- Android:自定义imageview实现两条线裁剪图片,不在区域内显示阴影
- Android圆形图片不求人,自定义View实现(BitmapShader使用)
- Android使用ViewFlipper实现图片切换功能
- android困惑之UI---imageView实现对图片的选取,和裁剪功能。。。。
- Android 自定义View修炼-Android实现圆形、圆角和椭圆自定义图片View(使用BitmapShader图形渲染方法)
- 【Android 开发】:UI控件之 ImageView 实现适屏和裁剪图片的功能
- Android最简单的图片裁剪方法,非使用系统裁切功能
- android 自定义view使用Canvas实现支付宝咻一咻功能
- Android客户端之“微服私访”App的系统学习(五)使用Picasso实现轮播图以及使用自定义View实现个人中心页面
- Android使用自定义属性实现图片自动播放滚动的功能
- Android自定义progressDialog使用系统ProgressBar与图片实现(一)系统ProgressBar
- Android使用ViewPager、PhotoView实现类似QQ空间图片浏览功能
- Android中使用自定义的view实现圆形图片的效果
- 区域实现Android实现图片的裁剪(不调用系统功能)
- Android圆形图片不求人,自定义View实现(BitmapShader使用)
- Android自定义 view之图片裁剪从设计到实现