您的位置:首页 > 其它

2D图形截取

2015-12-11 23:35 253 查看
从昨天开始 学第九章内容 ,寒假放假之前必须过一遍!

今天做的是2D的图片处理截取 ,虽然不是太难但是注意的点多了,以后没准能用得到,记下来吧。

1、 XML

<span style="font-size:18px;color:#ff6600;"><?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/frameLayout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>

<ImageView
android:layout_marginTop="0dp"
android:id="@+id/imageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>

<TextView
android:id="@+id/textviewshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示坐标"
android:textSize="20dp"
android:textColor="@color/colorAccent"
android:layout_gravity="left|bottom" />
</FrameLayout></span><span style="color:#ff0000;">
</span>


2、Java (一个)

<span style="font-size:14px;"><span style="color:#ff0000;">
</span><span style="color:#ff9900;">import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;</span><span style="color:#ff0000;">

</span><span style="color:#ff9900;">import org.w3c.dom.Text;</span><span style="color:#ff0000;">

</span><span style="color:#993300;">public class MainActivity extends AppCompatActivity {
private FrameLayout frameLayout;
private ImageView imageView;
private TextView textViewshow;

</span><span style="color:#ffcc00;">@Override</span><span style="color:#993300;">
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
frameLayout = (FrameLayout)findViewById(R.id.frameLayout_1);
imageView = (ImageView)findViewById(R.id.imageview);
textViewshow = (TextView)findViewById(R.id.textviewshow);

frameLayout.addView(new MyView(this));
</span><span style="color:#009900;">/**坐标显示方便调整图像大小
* */</span><span style="color:#993300;">
frameLayout.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
float x = event.getRawX();
float y = event.getRawY();
textViewshow.setText("当前坐标  X_" + x + "  " + "Y_" + y);
return false;
}
});
}
public class MyView extends View
{
public MyView(Context context)
{
super(context);
}

</span><span style="color:#ffcc00;">@Override</span><span style="color:#993300;">
protected void onDraw(Canvas canvas) {
Paint paint = new Paint();
String path = </span><span style="color:#ff0000;">"/data/NewFile/a3.jpg"</span><span style="color:#993300;">;</span><span style="color:#000066;">//自己需要往文件夹中放一张图片,路径还要加上png or jpg.</span><span style="color:#993300;">
Bitmap bitmap = BitmapFactory.decodeFile(path);</span><span style="color:#009900;">//获取通过解码得到的位图</span><span style="color:#993300;">
</span><span style="color:#009900;">/**
* public static Bitmap decodeFile (String pathName)

Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.
Parameters
pathName	complete path name for the file to be decoded.
Returns
the resulting decoded bitmap, or null if it could not be decoded.
* */</span><span style="color:#993300;">
canvas.drawBitmap(bitmap,0,30,paint);</span><span style="color:#33cc00;">//将获取的Bitmap对象绘制在画布上指定位置</span><span style="color:#993300;">
Rect src = new Rect(95,50,75,300);</span><span style="color:#009900;">//设置挖取的区域  可以为空,那么将整个照片进行截取show</span><span style="color:#993300;">
Rect dst = new Rect(420,30,500,120);</span><span style="color:#009900;">//设置绘制的区域</span><span style="color:#993300;">
canvas.drawBitmap(bitmap, src, dst, paint);</span><span style="color:#009900;">//绘制挖取到的图像</span><span style="color:#993300;">
</span><span style="color:#009900;">////使用颜色数组创建一个Bitmap对象</span><span style="color:#993300;">
Bitmap bitmap_1 = Bitmap.createBitmap(new int[]{Color.RED,Color.GREEN,Color.BLUE,Color.MAGENTA},4,1, Bitmap.Config.RGB_565);
imageView.setImageBitmap(bitmap_1);
super.onDraw(canvas);
}
}

</span><span style="color:#ffcc00;"> @Override</span><span style="color:#993300;">
protected void onDestroy() {
BitmapDrawable b = (BitmapDrawable)imageView.getDrawable();
if(b!= null && !b.getBitmap().isRecycled())
{
b.getBitmap().recycle();
}
super.onDestroy();
}
}
</span></span>


效果:


源代码:http://download.csdn.net/detail/csdnhejingzhou/9348707
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: