在GridView Android中使用自定义ImageView项
我正在用android开发一款记忆游戏 现在我需要创建一个大写网格,并且我正在使用一个自定义的Imageview作为网格中的项目 这是我的Cap对象类,它扩展了Imageview在GridView Android中使用自定义ImageView项,android,gridview,android-custom-view,Android,Gridview,Android Custom View,我正在用android开发一款记忆游戏 现在我需要创建一个大写网格,并且我正在使用一个自定义的Imageview作为网格中的项目 这是我的Cap对象类,它扩展了Imageview public class DhakkanObject extends ImageView { public List<Fruit> fruitMap = new ArrayList<Fruit>(); public ClosedState closedState; public OpenSta
public class DhakkanObject extends ImageView {
public List<Fruit> fruitMap = new ArrayList<Fruit>();
public ClosedState closedState;
public OpenState openState;
public AnimationState animationState;
public State<DhakkanObject> previousState;
public State<DhakkanObject> currentState;
public DhakkanObject(Context context) {
super(context);
closedState = new ClosedState();
openState = new OpenState();
animationState = new AnimationState();
}
public void gotoState(State<DhakkanObject> newState) {
if (newState == null) {
return;
} else if (currentState == newState) {
return;
}
if (currentState == null) {
currentState = newState;
}
if (previousState != null)
previousState.exit();
previousState = currentState;
currentState = newState;
currentState.enter();
invalidate();
}
public class OpenState extends State<DhakkanObject> {
@Override
public void enter() {
setBackgroundResource(R.drawable.open);
}
@Override
public void update(long gameTime) {
// TODO Auto-generated method stub
}
@Override
public void exit() {
// TODO Auto-generated method stub
}
}
public class ClosedState extends State<DhakkanObject>
{
@Override
public void enter() {
setBackgroundResource(R.drawable.closed);
}
@Override
public void update(long gameTime) {
// TODO Auto-generated method stub
}
@Override
public void exit() {
// TODO Auto-generated method stub
}
}
public class AnimationState extends State<DhakkanObject>{
public AnimationDrawable frameByframe_animation = null;
@Override
public void enter() {
setVisibility(View.VISIBLE);
setBackgroundResource(R.anim.dhakkan_animation);
frameByframe_animation = (AnimationDrawable) getBackground();
frameByframe_animation.start();
frameByframe_animation.setOneShot(true);
}
@Override
public void update(long gameTime) {
post(new Runnable() {
public void run() {
if(frameByframe_animation.isRunning()){
gotoState(openState);
}
}
});
}
@Override
public void exit() {
frameByframe_animation.stop();
frameByframe_animation.setVisible(false, false);
}
}
}
公共类DhakkaNoObject扩展了ImageView{
public List foultmap=new ArrayList();
公共关闭状态关闭状态;
公共开放州开放州;
公共动画状态动画状态;
公共国家优先国家;
公共国家;
公共DhakkanObject(上下文){
超级(上下文);
closedState=新的closedState();
openState=新的openState();
animationState=新的animationState();
}
公共无效gostate(State newState){
if(newState==null){
返回;
}else if(currentState==newState){
返回;
}
如果(currentState==null){
当前状态=新闻状态;
}
如果(previousState!=null)
previousState.exit();
previousState=当前状态;
当前状态=新闻状态;
currentState.enter();
使无效();
}
公共类OpenState扩展了状态{
@凌驾
公开作废输入(){
挫折资源(R.drawable.open);
}
@凌驾
公开作废更新(长游戏时间){
//TODO自动生成的方法存根
}
@凌驾
公共无效出口(){
//TODO自动生成的方法存根
}
}
公共类ClosedState扩展了状态
{
@凌驾
公开作废输入(){
setBackgroundResource(R.drawable.closed);
}
@凌驾
公开作废更新(长游戏时间){
//TODO自动生成的方法存根
}
@凌驾
公共无效出口(){
//TODO自动生成的方法存根
}
}
公共类AnimationState扩展了状态{
public AnimationDrawable逐帧_animation=null;
@凌驾
公开作废输入(){
设置可见性(View.VISIBLE);
挫折资源(R.anim.dhakkan_动画);
frameByframe_animation=(AnimationDrawable)getBackground();
frameByframe_animation.start();
逐帧动画.setOneShot(true);
}
@凌驾
公开作废更新(长游戏时间){
post(新的Runnable(){
公开募捐{
if(frameByframe\u animation.isRunning()){
gotoState(openState);
}
}
});
}
@凌驾
公共无效出口(){
逐帧_动画。停止();
frameByframe_animation.setVisible(false,false);
}
}
}
下面是我实现GridView的活动
public class DoubleBlastMainActivity extends Activity {
private Intent intent = null;
public GridView gameGrid;
public List<DhakkanObject> dhakkanList = null;
public int mLevel = 0;
public UpdateThread updateThread = null;
public DhakkanObject openDhakkan1;
public DhakkanObject openDhakkan2;
public DhakkanObject currentDhakkanClicked;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.double_blast_game);
gameGrid = ( GridView ) findViewById( R.id.dhakkanGrid );
dhakkanList = new ArrayList<DhakkanObject>();
createDhakkanList(2, 2);
gameGrid.setAdapter(new DhakkanGrid( getBaseContext(), dhakkanList) );
}
public void createDhakkanList(int numberOfCol, int numberOfRows)
{
for( int i = 0; i < ( numberOfRows * numberOfCol ); i++)
{
DhakkanObject temp = new DhakkanObject(getBaseContext());
temp.gotoState(temp.closedState);
temp.setScaleType(ScaleType.CENTER_CROP);
dhakkanList.add(temp);
}
gameGrid.setNumColumns(numberOfCol);
gameGrid.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
}
public class DhakkanGrid extends BaseAdapter
{
public Context context;
public List<DhakkanObject> dhakkhanList;
public DhakkanGrid ( Context contexttemp, List<DhakkanObject> objectlist)
{
this.context = contexttemp;
dhakkanList = objectlist;
}
public int getCount() {
// TODO Auto-generated method stub
return dhakkanList.size();
}
public Object getItem(int position) {
// TODO Auto-generated method stub
return dhakkanList.get(position);
}
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
return dhakkanList.get(position);
}
}
}
public类活动扩展活动{
私人意图=无效;
公共网格视图游戏网格;
公共列表dhakkanList=null;
公共int mLevel=0;
public UpdateThread UpdateThread=null;
公共DhakkanObject openDhakkan1;
公共DhakkanObject openDhakkan2;
公共DhakkanObject当前DhakkanClicked;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.double_blast_游戏);
gameGrid=(GridView)findViewById(R.id.dhakkanGrid);
dhakkanList=新数组列表();
createDhakkanList(2,2);
setAdapter(新的DhakkanGrid(getBaseContext(),dhakkanList));
}
public void createDhakkanList(int numberOfCol,int numberOfRows)
{
对于(int i=0;i<(numberOfRows*numberOfCol);i++)
{
DhakkanObject temp=新的DhakkanObject(getBaseContext());
温度稳定状态(温度关闭状态);
温度设置刻度类型(刻度型中心作物);
dhakkanList.add(临时);
}
gameGrid.setNumColumns(numberOfCol);
gameGrid.setStretchMode(GridView.STRETCH\u列宽);
}
公共类DhakkanGrid扩展BaseAdapter
{
公共语境;
公开名单;
公共DhakkanGrid(上下文上下文临时,列表对象列表)
{
this.context=contexttemp;
dhakkanList=对象列表;
}
public int getCount(){
//TODO自动生成的方法存根
返回dhakkanList.size();
}
公共对象getItem(int位置){
//TODO自动生成的方法存根
返回dhakkanList.get(位置);
}
公共长getItemId(int位置){
//TODO自动生成的方法存根
返回0;
}
公共视图getView(int位置、视图转换视图、视图组父视图){
//TODO自动生成的方法存根
返回dhakkanList.get(位置);
}
}
}
布局代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/game_bg"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/Score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/pauseMenu"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="false"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/Moves"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/pauseMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_marginRight="10dp"
android:text="Button" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</FrameLayout>
<GridView
android:id="@+id/dhakkanGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:numColumns="3" >
</GridView>
</RelativeLayout>
</LinearLayout>
结果是一个高度不均衡的网格。这意味着图像视图在宽度上呈指数级拉伸
你能告诉我我做错了什么吗?因为我似乎无法调整项目的大小 您在任何地方都使用了
setBackgroundResource
。请改用setImageResource
。我不确定是否理解您的问题。你可能应该在你的描述中贴一张图片
我会试着去推翻这个决定。如前所述,可以调整ImageView的大小。
首先使用getMeasuredWidth
和getMeasuredHeight
获取ImageView的尺寸。然后使用getDrawable
获取图像,并获取图像的尺寸。
现在,您有了正确的图像比率,将它们应用到ImageView
。
要应用新尺寸标注,请使用
您应该首先决定应该调整哪个维度的大小。但我无法帮你制作应用程序的图像
编辑1:
在Dhakkano对象中,添加以下函数:
@Override
public void onMeasure(int prefWidth, int prefHeight){
Drawable d = getDrawable();
if(d!=null){
int width = getMeasuredWidth(prefWidth);
int height = (int) Math.ceil((float) width * (float) d.getIntrinsicHeight() / (float) d.getIntrinsicWidth());
int prefWidthView = resolveSizeAndState(width, prefWidth, 0);
int prefHeightView = resolveSizeAndState(height, prefHeight, 0);
setMeasuredDimension(prefWidthView, prefHeightView);
}
else{
super.onMeasured(prefWidth,prefHeight);
}
}
每次调整imageview的大小时,imageview都会检查底层图像的尺寸,并扩展其高度以保持正确的比例,前提是第一次宽度很好。我使用了setImageResource,这似乎使问题更加严重。现在,高度和宽度都被拉伸了!我在帖子中添加了一个截图。