添加动态ImageView的查看 [英] add imageview dynamically to view

查看:157
本文介绍了添加动态ImageView的查看的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想提出的android游戏,使用视图class.i我不使用layout.all我的图片与canvas.Now我的问题是拉我不能给一个位图

可触摸事件,你知道。我正在尝试动态我的看法类添加到的ImageView

使用可触摸event.why我尝试动态地添加ImageView的?因为我没有使用

layout.but我可以not.is有任何机构帮助?

 这里是我的code:
包com.example.poolmaster;进口的java.util.ArrayList;进口android.annotation.Sup pressLint;
进口android.app.Activity;
进口android.content.Context;
进口android.graphics.Bitmap;
进口android.graphics.BitmapFactory;
进口android.graphics.Canvas;
进口android.graphics.Matrix;
进口android.graphics.drawable.BitmapDrawable;
进口android.graphics.drawable.Drawable;
进口android.os.Bundle;
进口android.util.DisplayMetrics;
进口android.view.Gravity;
进口android.view.Menu;
进口android.view.MotionEvent;
进口android.view.View;
进口android.view.View.OnTouchListener;
进口android.widget.ImageView;
进口android.widget.LinearLayout;公共类MainActivity延伸活动{
    私人诠释cuePosx,cuePosy;
    INT cueHeight,cueWeight;
    双rotatingAngle;
    INT高度;
    INT wwidth;
    INT cueHeight2,cueWeight2;
    位图表格,贴,提高;
    ImageView的按钮=新ImageView的(本);
    @覆盖
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        rotatingAngle = 0;
        DisplayMetrics displaymetrics =新DisplayMetrics();
        。getWindowManager()getDefaultDisplay()getMetrics(displaymetrics)。
         高度= displaymetrics.heightPixels;
         wwidth = displaymetrics.widthPixels;
         cueHeight2 =身高/ 2;
         cueWeight2 = wwidth / 2;
        的System.out.println(************);
        的System.out.println(高度+高);
        的System.out.println(权重+ wwidth);
        的System.out.println(************);
        //设置通用布局参数
         //这个修改        游戏的自定义=新的游戏(本);
        的setContentView(自定义);
       //的setContentView(R.layout.activity_main);
    }    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        。getMenuInflater()膨胀(R.menu.activity_main,菜单);
        返回true;
    }
    @覆盖
    公共布尔onTouchEvent(MotionEvent EV){
        如果(ev.getAction()== MotionEvent.ACTION_MOVE){
            // cuePosy + = 10;
            //System.out.println(\"xCORDİNATES!!!!+ ev.getX());
            //System.out.println(\"yCORDİNATES!!!!+ ev.getY());
            rotatingAngle = getAngle(ev.getX(),ev.getY());
            的System.out.println(角度+ rotatingAngle);        }
        如果(ev.getAction()== MotionEvent.ACTION_DOWN){
            的System.out.println(******************操作下****************);
            // cueHeight2 + = 10;
            // cueWeight2 + = 20;
            // cuePosy = 320;
        }
        如果(ev.getAction()== MotionEvent.ACTION_UP){
            的System.out.println(******************操作下****************);
            // cueHeight2- = 10;
            // cueWeight2- = 20;
            // cuePosy = 320;
        }
        返回true;
    }
    私人双getAngle(双xTouch,双yTouch){
        双THETA;        THETA = Math.toDegrees(Math.atan2(高度/ 2-yTouch,xTouch-wwidth / 2));        返回THETA;    }    公共类游戏扩展视图{        公共游戏(上下文的背景下){
            超级(上下文);
            // TODO自动生成构造函数存根
            表= BitmapFactory.de codeResource(getResources(),R.drawable.bg);
            表= Bitmap.createScaledBitmap(表,wwidth,高度,真);
            坚持= BitmapFactory.de codeResource(getResources(),R.drawable.stick);            募集= BitmapFactory.de codeResource(getResources(),R.drawable.raise);
            cueHeight = stick.getHeight();
            的System.out.println(ıstaka+ cueHeight);
            cueWeight = stick.getWidth();
            cuePosx = wwidth / 2;
            cuePosy =高度cueHeight-180;
        }
          @燮pressLint({DrawAllocation,DrawAllocation})
        @覆盖
            公共无效的onDraw(帆布油画){
                super.onDraw(画布);                字模=新的Matrix();
                matrix.setTranslate(cueWeight2,cueHeight2);
                matrix.postRotate((浮点)rotatingAngle,cueWeight2,cueHeight2); //逆时针90度                //使用矩阵变换的结果创建从原来的新位图
                位图rotatedBitmap = Bitmap.createBitmap(棒,0,0,坚持.getWidth(),坚持.getHeight(),基质,FALSE);
                canvas.save(); //保存画布的位置。
                canvas.restore();                    //旋转画布。
              canvas.drawBitmap(表,0,0,NULL); //绘制旋转画布上球。
              canvas.drawBitmap(棒,矩阵,NULL);
              canvas.drawBitmap(提高,0,0,NULL);
              无效();
          }    }
}


解决方案

这对于Activity类扩展添加的ImageView到布局

 的LinearLayout为1L = findViewById(R.id.xmlfile_layout_id);   ImageView的imgView =新ImageView的(上下文);   imgView.setVisibility(View.VISIBLE);
   lL.addView(imgView);

这是添加的ImageView到View类扩展画布

首先,你可以不使用帆布放置任何的ImageView,编辑文本或按钮。相反,你必须把它画。因此,创建一个自定义布局,并绘制布局帆布

试试这个,它可以帮助你。在的onDraw(..)

 的LinearLayout为1L =新的LinearLayout(背景);   ImageView的imgView =新ImageView的(上下文);   imgView.setVisibility(View.VISIBLE);
   lL.addView(imgView);    lL.measure(canvas.getWidth(),canvas.getHeight());
    lL.layout(0,0,canvas.getWidth(),canvas.getHeight());    //放置在特定坐标编辑文本:
    //canvas.translate(0,0);
    layout.draw(画布);

和看看另外一个例子:点击这里

它给添加视图的另一种方式。

i am making android game which use view class.i am not using layout.all my images is drawing with canvas.Now my problem is i cannot give a bitmap

touchable event as you know.i am trying to add imageview dynamically to my view class to

use touchable event.why i try to add imageview dynamically?Because i am not using

layout.but i could not.is there any body to help?

here is my code:


package com.example.poolmaster;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
    private int cuePosx,cuePosy;
    int cueHeight,cueWeight;
    double rotatingAngle;
    int height;
    int wwidth;
    int cueHeight2,cueWeight2;
    Bitmap table,stick,raise;
    ImageView button = new ImageView(this);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        rotatingAngle=0;
        DisplayMetrics displaymetrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
         height = displaymetrics.heightPixels;
         wwidth = displaymetrics.widthPixels;
         cueHeight2=height/2;
         cueWeight2=wwidth/2;
        System.out.println("**************************************");
        System.out.println("height " +height);
        System.out.println("weight" +wwidth);
        System.out.println("**************************************");


        // Set generic layout parameters
         // Modify this

        GamePlay custom = new GamePlay(this);


        setContentView(custom);


       // setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {


        if(ev.getAction() == MotionEvent.ACTION_MOVE){
            //cuePosy+=10;
            //System.out.println("xCORDİNATES!!!! " +ev.getX());
            //System.out.println("yCORDİNATES!!!! " +ev.getY());
            rotatingAngle=getAngle(ev.getX(), ev.getY());
            System.out.println("Angle " +rotatingAngle);

        }
        if(ev.getAction()==MotionEvent.ACTION_DOWN){
            System.out.println("****************** ACTİON DOWN ****************");
            //cueHeight2+=10;
            //cueWeight2+=20;
            //cuePosy=320;
        }
        if(ev.getAction()==MotionEvent.ACTION_UP){
            System.out.println("****************** ACTİON DOWN ****************");
            //cueHeight2-=10;
            //cueWeight2-=20;
            //cuePosy=320;
        }
        return true;
    }
    private double getAngle(double xTouch, double yTouch) {
        double theta;

        theta=  Math.toDegrees( Math.atan2( height/2-yTouch , xTouch-wwidth/2) );

        return theta;

    }



    public class GamePlay extends View{

        public GamePlay(Context context) {
            super(context);
            // TODO Auto-generated constructor stub
            table=BitmapFactory.decodeResource(getResources(), R.drawable.bg);
            table= Bitmap.createScaledBitmap(table, wwidth, height, true);
            stick=BitmapFactory.decodeResource(getResources(), R.drawable.stick);

            raise=BitmapFactory.decodeResource(getResources(), R.drawable.raise);
            cueHeight=stick.getHeight();
            System.out.println("ıstaka " +cueHeight);
            cueWeight=stick.getWidth();
            cuePosx=wwidth/2;
            cuePosy=height-cueHeight-180;
        }
          @SuppressLint({ "DrawAllocation", "DrawAllocation" })
        @Override
            public void onDraw(Canvas canvas) {
                super.onDraw(canvas);

                Matrix matrix = new Matrix();
                matrix.setTranslate(cueWeight2, cueHeight2);
                matrix.postRotate((float)rotatingAngle,cueWeight2, cueHeight2); // anti-clockwise by 90 degrees

                // create a new bitmap from the original using the matrix to transform the result
                Bitmap rotatedBitmap = Bitmap.createBitmap(stick , 0, 0, stick .getWidth(), stick .getHeight(), matrix,false);


                canvas.save(); //Save the position of the canvas.
                canvas.restore();

                    //Rotate the canvas.
              canvas.drawBitmap(table, 0, 0, null); //Draw the ball on the rotated canvas.
              canvas.drawBitmap(stick, matrix,null);
              canvas.drawBitmap(raise, 0, 0,null);
              invalidate();


          }

    }
}

解决方案

This is for adding imageview to a layout where Activity class is extended

   LinearLayout lL = findViewById(R.id.xmlfile_layout_id);

   ImageView imgView = new ImageView(context); 

   imgView.setVisibility(View.VISIBLE);
   lL.addView(imgView);

This is for adding imageview to a canvas where View class is extended

Initially, you cannot place any imageview, edit text or buttons using canvas. Instead, you have to draw it. So create a custom layout and draw that layout with canvas

Try this, It might help you. in onDraw(..)

   LinearLayout lL = new LinearLayout(context);

   ImageView imgView = new ImageView(context); 

   imgView.setVisibility(View.VISIBLE);
   lL.addView(imgView);

    lL.measure(canvas.getWidth(), canvas.getHeight());
    lL.layout(0, 0, canvas.getWidth(), canvas.getHeight());

    // placing the edit text at specific co-ordinates:
    //canvas.translate(0, 0);
    layout.draw(canvas);

And take a look at this another example : Click here

It gives another way of adding views

这篇关于添加动态ImageView的查看的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆