2D图形截取

从昨天开始 学第九章内容   ,寒假放假之前必须过一遍!


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

1、 XML




    

    




2、Java (一个)



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;

import org.w3c.dom.Text;

public class MainActivity extends AppCompatActivity {
    private FrameLayout frameLayout;
    private ImageView imageView;
    private TextView textViewshow;

    @Override
    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));
        /**坐标显示方便调整图像大小
         * */
        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);
        }

        @Override
        protected void onDraw(Canvas canvas) {
            Paint paint = new Paint();
            String path = "/data/NewFile/a3.jpg";//自己需要往文件夹中放一张图片,路径还要加上png or jpg.
            Bitmap bitmap = BitmapFactory.decodeFile(path);//获取通过解码得到的位图
            /**
             * 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.
             * */
            canvas.drawBitmap(bitmap,0,30,paint);//将获取的Bitmap对象绘制在画布上指定位置
            Rect src = new Rect(95,50,75,300);//设置挖取的区域  可以为空,那么将整个照片进行截取show
            Rect dst = new Rect(420,30,500,120);//设置绘制的区域
            canvas.drawBitmap(bitmap, src, dst, paint);//绘制挖取到的图像
            ////使用颜色数组创建一个Bitmap对象
            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);
        }
    }

    @Override
    protected void onDestroy() {
        BitmapDrawable b = (BitmapDrawable)imageView.getDrawable();
        if(b!= null && !b.getBitmap().isRecycled())
        {
            b.getBitmap().recycle();
        }
        super.onDestroy();
    }
}



效果: 2D图形截取_第1张图片


源代码:http://download.csdn.net/detail/csdnhejingzhou/9348707


你可能感兴趣的:(Android)