移动应用开发:1.使用Android的图形处理的基本操作技能 a)绘制图片500×500,左空隙10dp,在图片底部添上文字“Hello Android” b)绘制点(10,550),(800,1000),点宽50,颜色为黄色; c)绘制绿色线(10,550)到(800,1000),线宽10;红色多线(10,550)到(10,1000), (10,1000)到(800,1000) , (800,1000)到 (800, 550) ,(800,550)到(10,550) d)绘制矩形,填充色CYAN, (10, 1050, 350, 1500) 2.实现简易订餐系统的菜品详情,同时切入图片的动画特效 a)实现点击菜品项目,启动新的页面,布局包括标题和单价,图片,详情。 b)通过程序实现图片的动画加载效果,效果包含图片中心旋转并放大,持续事件1秒钟。 c)点击图片,弹出图片对话框。图片可以通过双指触屏放大缩小
时间: 2024-02-13 09:06:26 浏览: 95
基于Android图片处理1
对于第一个问题,以下是绘制图片、点、线、多线和矩形的代码示例:
```java
public class MyView extends View {
private Paint mPaint;
public MyView(Context context) {
super(context);
mPaint = new Paint();
mPaint.setAntiAlias(true);
}
@Override
public void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 绘制图片
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);
Rect dstRect = new Rect(10, 10, 510, 510);
canvas.drawBitmap(bitmap, null, dstRect, mPaint);
// 绘制文字
mPaint.setTextSize(50);
mPaint.setColor(Color.BLACK);
canvas.drawText("Hello Android", 20, 570, mPaint);
// 绘制点
mPaint.setColor(Color.YELLOW);
mPaint.setStrokeWidth(50);
canvas.drawPoint(10, 550, mPaint);
canvas.drawPoint(800, 1000, mPaint);
// 绘制线
mPaint.setColor(Color.GREEN);
mPaint.setStrokeWidth(10);
canvas.drawLine(10, 550, 800, 1000, mPaint);
// 绘制多线
mPaint.setColor(Color.RED);
mPaint.setStrokeWidth(10);
Path path = new Path();
path.moveTo(10, 550);
path.lineTo(10, 1000);
path.lineTo(800, 1000);
path.lineTo(800, 550);
path.lineTo(10, 550);
canvas.drawPath(path, mPaint);
// 绘制矩形
mPaint.setColor(Color.CYAN);
RectF rectF = new RectF(10, 1050, 350, 1500);
canvas.drawRect(rectF, mPaint);
}
}
```
对于第二个问题,以下是实现简易订餐系统的菜品详情,并切入图片的动画特效的代码示例:
```java
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.image);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showImageDialog();
}
});
}
private void showImageDialog() {
final Dialog dialog = new Dialog(this, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_image);
dialog.setCancelable(true);
ImageView imageView = dialog.findViewById(R.id.image);
AnimationSet animationSet = new AnimationSet(true);
animationSet.addAnimation(new ScaleAnimation(0, 1.2f, 0, 1.2f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.addAnimation(new RotateAnimation(0, 360, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f));
animationSet.setDuration(1000);
animationSet.setFillAfter(true);
imageView.startAnimation(animationSet);
imageView.setOnTouchListener(new View.OnTouchListener() {
private float lastDistance;
private float lastX;
private float lastY;
private int mode = 0;
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_DOWN:
mode = 1;
lastX = event.getX();
lastY = event.getY();
break;
case MotionEvent.ACTION_POINTER_DOWN:
mode = 2;
lastDistance = getDistance(event);
break;
case MotionEvent.ACTION_MOVE:
if (mode == 2) {
float distance = getDistance(event);
float scale = distance / lastDistance;
imageView.setScaleX(imageView.getScaleX() * scale);
imageView.setScaleY(imageView.getScaleY() * scale);
lastDistance = distance;
} else if (mode == 1) {
float x = event.getX();
float y = event.getY();
float deltaX = x - lastX;
float deltaY = y - lastY;
imageView.setTranslationX(imageView.getTranslationX() + deltaX);
imageView.setTranslationY(imageView.getTranslationY() + deltaY);
lastX = x;
lastY = y;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
mode = 0;
break;
}
return true;
}
private float getDistance(MotionEvent event) {
float x = event.getX(0) - event.getX(1);
float y = event.getY(0) - event.getY(1);
return (float) Math.sqrt(x * x + y * y);
}
});
dialog.show();
}
}
```
其中,`R.layout.activity_main` 是包含菜品列表的布局文件,`R.layout.dialog_image` 是包含放大的图片的布局文件。在菜品列表中,每个菜品项都设置了点击事件,点击后会弹出图片对话框。在图片对话框中,图片会执行动画特效并可以通过双指触屏放大缩小。
阅读全文