我该如何充气自定义视图中的XML?自定义、我该、视图、XML

2023-09-08 08:50:14 作者:伱說牽過手就算是約定

这是我第一次尝试使用XML自定义视图,我不知道如何做到这一点。任何帮助将大大AP preciated。

我使用 Interfuser的的CoverFlow 并需要抬高它,我可以使用它在一个XML格式,这样我就可以在视图中添加其他成分。

希望下面的code会告诉我需要什么(要做)。我相信这是很容易,我认为。

日Thnx

布局:

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=横向>

< com.myapp.home.CoverFlow
    机器人:ID =@ + ID / coverFlow1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =10dip/>

< / LinearLayout中>
 

CoverFlow.java:

 包com.myapp.home;

公共类的CoverFlow扩展库{

/ **
 *图形相机用于转化ImageViews的矩阵
 * /
私人相机mCamera =新相机();

/ **
 *最大角度儿童ImageView的将由旋转
 * /
私人诠释mMaxRotationAngle = 60;

/ **
 *中央孩子在最大变焦
 * /
私人诠释mMaxZoom = -120;

/ **
 *该的CoverFlow的中心
 * /
私人诠释mCoveflowCenter;

公众的CoverFlow(上下文的背景下){
超(上下文);
this.setStaticTransformationsEnabled(真正的);
}

公众的CoverFlow(上下文的背景下,ATTRS的AttributeSet){
超(背景下,ATTRS);
    this.setStaticTransformationsEnabled(真正的);
}

公众的CoverFlow(上下文的背景下,ATTRS的AttributeSet,诠释defStyle){
超(背景下,ATTRS,defStyle);
this.setStaticTransformationsEnabled(真正的);
}

/ **
 *获取图像的最大旋转角
返回:该mMaxRotationAngle
* /
公众诠释getMaxRotationAngle(){
返回mMaxRotationAngle;
}

/ **
*设置每个图像的最大旋转角度
*参数maxRotationAngle的mMaxRotationAngle设置
* /
公共无效setMaxRotationAngle(INT maxRotationAngle){
mMaxRotationAngle = maxRotationAngle;
}

/ **
*获取中间图像的最大变焦
返回:该mMaxZoom
* /
公众诠释getMaxZoom(){
返回mMaxZoom;
}

/ **
*设置中心图像的最大缩放
*参数maxZoom的mMaxZoom设置
* /
公共无效setMaxZoom(INT maxZoom){
mMaxZoom = maxZoom;
}

/ **
 *获取的CoverFlow的中心
 返回:此的CoverFlow的中心。
 * /
私人诠释getCenterOfCoverflow(){
    返回(的getWidth() -  getPaddingLeft() -  getPaddingRight())/ 2 + getPaddingLeft();
}

/ **
 *获取视图的中心
 返回:给定视图的中心。
 * /
私有静态诠释getCenterOfView(查看视图){
    返回view.getLeft()+ view.getWidth()/ 2;
}
/ **
* {@inheritDoc}
*
* @see #setStaticTransformationsEnabled(布尔)
* /
保护布尔getChildStaticTransformation(查看孩子,变换T){

最终诠释childCenter = getCenterOfView(子);
最终诠释childWidth = child.getWidth();
INT rotationAngle = 0;

t.clear();
t.se​​tTransformationType(Transformation.TYPE_MATRIX);

    如果(childCenter == mCoveflowCenter){
        transformImageBitmap((ImageView的)孩子,T,0);
    } 其他 {
        rotationAngle =(INT)(((浮动)(mCoveflowCenter  -  childCenter)/ childWidth)* mMaxRotationAngle);
        如果(Math.abs(rotationAngle)> mMaxRotationAngle){
         rotationAngle =(rotationAngle℃,)? -mMaxRotationAngle:mMaxRotationAngle;
        }
        transformImageBitmap((ImageView的)孩子,T,rotationAngle);
    }

返回true;
}

/ **
*这是布局过程调用时该视图的大小发生了变化。如果
*你刚才添加到视图层次,你所谓的老
* 0值。
*
* @参数W该视图当前宽度。
*参数•当前这种观点的高度。
* @参数oldw这种观点的旧宽度。
* @参数oldh这种观点的旧高度。
 * /
 保护无效onSizeChanged(INT W,INT小时,INT oldw,诠释oldh){
  mCoveflowCenter = getCenterOfCoverflow();
  super.onSizeChanged(W,H,oldw,oldh);
 }

 / **
  *通过通过角度变换的图像位图
  *
  *参数的ImageView ImageView的,我们要旋转其位图的ImageView
  * @参数万吨改造
  *参数rotationAngle角度通过它旋转的位图
  * /
 私人无效transformImageBitmap(ImageView的孩子,变换T,诠释rotationAngle){
  mCamera.save();
  最终矩阵imageMatrix = t.getMatrix();;
  最终诠释imageHeight = child.getLayoutParams()。高度;;
  。最终诠释ImageWidth等= child.getLayoutParams()宽度;
  最终诠释旋转= Math.abs(rotationAngle);

  mCamera.translate(0.0,0.0,100.0f);

  //作为视图的角度变得更小,放大
  如果(旋转< mMaxRotationAngle){
   浮动zoomAmount =(浮点)(mMaxZoom +(旋转* 1.5));
   mCamera.translate(0.0,0.0,zoomAmount);
  }

  mCamera.rotateY(rotationAngle);
  mCamera.getMatrix(imageMatrix);
  。imageMatrix preTranslate( - (ImageWidth等/ 2), - (imageHeight / 2));
  imageMatrix.postTranslate((ImageWidth等/ 2),(imageHeight / 2));
  mCamera.restore();
}
}
 
Excel 中的自定义视图怎么用

Main.java

 公共类主要扩展活动{

@覆盖
 公共无效的onCreate(包savedInstanceState){
 super.onCreate(savedInstanceState);

 requestWindowFeature(Window.FEATURE_NO_TITLE);

 的CoverFlow的CoverFlow;
 的CoverFlow =新的CoverFlow(本);

 coverFlow.setAdapter(新ImageAdapter(本));

 ImageAdapter coverImageAdapter =新ImageAdapter(本);

 coverImageAdapter.createReflectedImages();

 coverFlow.setAdapter(coverImageAdapter);

 coverFlow.setSpacing(-25);
 coverFlow.setSelection(4,真);
 coverFlow.setAnimationDuration(1000);


 的setContentView(R.layout.main);
 }

公共类ImageAdapter扩展了BaseAdapter {
 INT mGalleryItemBackground;
 私人语境mContext;

 私人的FileInputStream FIS;

 私人整数[] mImageIds = {
   R.drawable.a,
         R.drawable.b,
         R.drawable.c,
         R.drawable.d,
         R.drawable.e,
         R.drawable.f,
         R.drawable.g,
         R.drawable.h,
         R.drawable.i
 };

 私人的ImageView [] mImages;

 公共ImageAdapter(上下文C){
  mContext = C;
  mImages =新的ImageView [mImageIds.length]
 }
公共布尔createReflectedImages(){
      //间隙我们想要的反射和原始图像之间
      最终诠释reflectionGap = 4;


      INT索引= 0;
      对于(INT imageId:mImageIds){
    位图originalImage = BitmapFactory.de codeResource(getResources()
      imageId);
       INT宽度= originalImage.getWidth();
       INT高= originalImage.getHeight();


       //这不会规模但将翻转在Y轴
       字模=新的Matrix();
       矩阵preSCALE(1,-1)。

       //创建一个具有翻转矩阵应用了位图。
       //我们只想底部的图像的一半
       位图reflectionImage = Bitmap.createBitmap(originalImage,0,高度/ 2,宽度,高度/ 2,矩阵,假);


       //创建一个新位图相同的宽度,但高以适应反思
       位图bitmapWithReflection = Bitmap.createBitmap(宽
         ,(高度+高度/ 2),Config.ARGB_8888);

      //创建一个新的画布与位图,它是足够大的
      //图像加上间隙加反思
      帆布油画=新的Canvas(bitmapWithReflection);
      在原始图像//绘制
      canvas.drawBitmap(originalImage,0,0,NULL);
      在间隙//绘制
      油漆deafaultPaint =新的油漆();
      canvas.drawRect(0,高度,宽度,高度+ reflectionGap,deafaultPaint);
      在反射//绘制
      canvas.drawBitmap(reflectionImage,0,身高+ reflectionGap,NULL);

      //创建着色器是覆盖反射的线性梯度
      涂料粉刷=新的油漆();
      的LinearGradient着色=新的LinearGradient(0,originalImage.getHeight(),0,
        bitmapWithReflection.getHeight()+ reflectionGap,0x70ffffff,至0x00FFFFFF,
        TileMode.CLAMP);
      //设置油漆使用着色器(线性梯度)
      paint.setShader(着色);
      //设置传输模式是搬运工达夫和目的地
      paint.setXfermode(新PorterDuffXfermode(Mode.DST_IN));
      //使用描绘一个矩形,与我们的线性渐变
      canvas.drawRect(0,高度,宽度,
        bitmapWithReflection.getHeight()+ reflectionGap,油漆);

      ImageView的ImageView的=新ImageView的(mContext);
      imageView.setImageBitmap(bitmapWithReflection);
      imageView.setLayoutParams(新CoverFlow.LayoutParams(120,180));
      imageView.setScaleType(ScaleType.MATRIX);
      mImages [指数++] = ImageView的;

      }
   返回true;
 }

 公众诠释getCount将(){
     返回mImageIds.length;
 }

 公共对象的getItem(INT位置){
     返回的位置;
 }

 众长getItemId(INT位置){
     返回的位置;
 }

 公共查看getView(INT位置,查看convertView,ViewGroup中父){

  //如果你想从资源加载使用此code
     ImageView的我=新ImageView的(mContext);
     i.setImageResource(mImageIds [位置]);
     i.setLayoutParams(新CoverFlow.LayoutParams(130,130));
     i.setScaleType(ImageView.ScaleType.CENTER_INSIDE);

     //确保我们设置抗锯齿,否则我们得到锯齿
     BitmapDrawable绘制=(BitmapDrawable)i.getDrawable();
     drawable.setAntiAlias​​(真正的);
     返回我;

  //返回mImages [位置]
 }
 / **返回的视图的大小(0.0至1.0F)
  *根据偏移的中心。 * /
  公众持股量中的getScale(布尔专注,诚信偏移){
    / *式:1 /(2 ^偏移)* /
      返回Math.max(0,1.0F /(浮点)Math.pow(2,Math.abs(偏移)));
  }

}
}
 

解决方案

  LayoutInflater吹气=(LayoutInflater)本
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
最终的视图V = inflater.inflate(R.layout布局在这里,空,假的。);
 

和使用,如:

  TextView的电视=(TextView中)v.findViewById(R,ID为ID在这里。);
 

This is my first time trying to use a custom view in XML and I am not sure on how to do it. Any help would be greatly appreciated.

I am using Interfuser's coverflow and need to inflate it where I can use it in a XML format so i can add other components in the view.

Hopefully the code below will show what I need(trying to do). I am sure this is easier than i think.

Thnx

Layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >

<com.myapp.home.CoverFlow
    android:id="@+id/coverFlow1"
    android:layout_width="wrap_content"
    android:layout_height="10dip" />

</LinearLayout>

CoverFlow.java:

package com.myapp.home;

public class CoverFlow extends Gallery {

/**
 * Graphics Camera used for transforming the matrix of ImageViews
 */
private Camera mCamera = new Camera();

/**
 * The maximum angle the Child ImageView will be rotated by
 */    
private int mMaxRotationAngle = 60;

/**
 * The maximum zoom on the centre Child
 */
private int mMaxZoom = -120;

/**
 * The Centre of the Coverflow 
 */   
private int mCoveflowCenter;

public CoverFlow(Context context) {
super(context);
this.setStaticTransformationsEnabled(true);
}

public CoverFlow(Context context, AttributeSet attrs) {
super(context, attrs);
    this.setStaticTransformationsEnabled(true);
}

public CoverFlow(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
this.setStaticTransformationsEnabled(true);   
}

/**
 * Get the max rotational angle of the image
* @return the mMaxRotationAngle
*/
public int getMaxRotationAngle() {
return mMaxRotationAngle;
}

/**
* Set the max rotational angle of each image
* @param maxRotationAngle the mMaxRotationAngle to set
*/
public void setMaxRotationAngle(int maxRotationAngle) {
mMaxRotationAngle = maxRotationAngle;
}

/**
* Get the Max zoom of the centre image
* @return the mMaxZoom
*/
public int getMaxZoom() {
return mMaxZoom;
}

/**
* Set the max zoom of the centre image
* @param maxZoom the mMaxZoom to set
*/
public void setMaxZoom(int maxZoom) {
mMaxZoom = maxZoom;
}

/**
 * Get the Centre of the Coverflow
 * @return The centre of this Coverflow.
 */
private int getCenterOfCoverflow() {
    return (getWidth() - getPaddingLeft() - getPaddingRight()) / 2 + getPaddingLeft();
}

/**
 * Get the Centre of the View
 * @return The centre of the given view.
 */
private static int getCenterOfView(View view) {
    return view.getLeft() + view.getWidth() / 2;
}  
/**
* {@inheritDoc}
*
* @see #setStaticTransformationsEnabled(boolean) 
*/ 
protected boolean getChildStaticTransformation(View child, Transformation t) {

final int childCenter = getCenterOfView(child);
final int childWidth = child.getWidth() ;
int rotationAngle = 0;

t.clear();
t.setTransformationType(Transformation.TYPE_MATRIX);

    if (childCenter == mCoveflowCenter) {
        transformImageBitmap((ImageView) child, t, 0);
    } else {      
        rotationAngle = (int) (((float) (mCoveflowCenter - childCenter)/ childWidth) *      mMaxRotationAngle);
        if (Math.abs(rotationAngle) > mMaxRotationAngle) {
         rotationAngle = (rotationAngle < 0) ? -mMaxRotationAngle : mMaxRotationAngle;   
        }
        transformImageBitmap((ImageView) child, t, rotationAngle);         
    }    

return true;
}

/**
* This is called during layout when the size of this view has changed. If
* you were just added to the view hierarchy, you're called with the old
* values of 0.
*
* @param w Current width of this view.
* @param h Current height of this view.
* @param oldw Old width of this view.
* @param oldh Old height of this view.
 */
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
  mCoveflowCenter = getCenterOfCoverflow();
  super.onSizeChanged(w, h, oldw, oldh);
 }

 /**
  * Transform the Image Bitmap by the Angle passed 
  * 
  * @param imageView ImageView the ImageView whose bitmap we want to rotate
  * @param t transformation 
  * @param rotationAngle the Angle by which to rotate the Bitmap
  */
 private void transformImageBitmap(ImageView child, Transformation t, int rotationAngle) {            
  mCamera.save();
  final Matrix imageMatrix = t.getMatrix();;
  final int imageHeight = child.getLayoutParams().height;;
  final int imageWidth = child.getLayoutParams().width;
  final int rotation = Math.abs(rotationAngle);

  mCamera.translate(0.0f, 0.0f, 100.0f);

  //As the angle of the view gets less, zoom in     
  if ( rotation < mMaxRotationAngle ) {
   float zoomAmount = (float) (mMaxZoom +  (rotation * 1.5));
   mCamera.translate(0.0f, 0.0f, zoomAmount);          
  } 

  mCamera.rotateY(rotationAngle);
  mCamera.getMatrix(imageMatrix);               
  imageMatrix.preTranslate(-(imageWidth/2), -(imageHeight/2)); 
  imageMatrix.postTranslate((imageWidth/2), (imageHeight/2));
  mCamera.restore();
}
}

Main.java

public class Main extends Activity {

@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 requestWindowFeature(Window.FEATURE_NO_TITLE);

 CoverFlow coverFlow;
 coverFlow = new CoverFlow(this);

 coverFlow.setAdapter(new ImageAdapter(this));

 ImageAdapter coverImageAdapter =  new ImageAdapter(this);

 coverImageAdapter.createReflectedImages();

 coverFlow.setAdapter(coverImageAdapter);

 coverFlow.setSpacing(-25);
 coverFlow.setSelection(4, true);
 coverFlow.setAnimationDuration(1000);


 setContentView(R.layout.main);
 }

public class ImageAdapter extends BaseAdapter {
 int mGalleryItemBackground;
 private Context mContext;

 private FileInputStream fis;

 private Integer[] mImageIds = {
   R.drawable.a,
         R.drawable.b,
         R.drawable.c,
         R.drawable.d,
         R.drawable.e,
         R.drawable.f,
         R.drawable.g,
         R.drawable.h,
         R.drawable.i
 };

 private ImageView[] mImages;

 public ImageAdapter(Context c) {
  mContext = c;
  mImages = new ImageView[mImageIds.length];
 }
public boolean createReflectedImages() {
      //The gap we want between the reflection and the original image
      final int reflectionGap = 4;


      int index = 0;
      for (int imageId : mImageIds) {
    Bitmap originalImage = BitmapFactory.decodeResource(getResources(), 
      imageId);
       int width = originalImage.getWidth();
       int height = originalImage.getHeight();


       //This will not scale but will flip on the Y axis
       Matrix matrix = new Matrix();
       matrix.preScale(1, -1);

       //Create a Bitmap with the flip matrix applied to it.
       //We only want the bottom half of the image
       Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height/2, width, height/2, matrix, false);


       //Create a new bitmap with same width but taller to fit reflection
       Bitmap bitmapWithReflection = Bitmap.createBitmap(width 
         , (height + height/2), Config.ARGB_8888);

      //Create a new Canvas with the bitmap that's big enough for
      //the image plus gap plus reflection
      Canvas canvas = new Canvas(bitmapWithReflection);
      //Draw in the original image
      canvas.drawBitmap(originalImage, 0, 0, null);
      //Draw in the gap
      Paint deafaultPaint = new Paint();
      canvas.drawRect(0, height, width, height + reflectionGap, deafaultPaint);
      //Draw in the reflection
      canvas.drawBitmap(reflectionImage,0, height + reflectionGap, null);

      //Create a shader that is a linear gradient that covers the reflection
      Paint paint = new Paint(); 
      LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, 
        bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, 
        TileMode.CLAMP); 
      //Set the paint to use this shader (linear gradient)
      paint.setShader(shader); 
      //Set the Transfer mode to be porter duff and destination in
      paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); 
      //Draw a rectangle using the paint with our linear gradient
      canvas.drawRect(0, height, width, 
        bitmapWithReflection.getHeight() + reflectionGap, paint); 

      ImageView imageView = new ImageView(mContext);
      imageView.setImageBitmap(bitmapWithReflection);
      imageView.setLayoutParams(new CoverFlow.LayoutParams(120, 180));
      imageView.setScaleType(ScaleType.MATRIX);
      mImages[index++] = imageView;

      }
   return true;
 }

 public int getCount() {
     return mImageIds.length;
 }

 public Object getItem(int position) {
     return position;
 }

 public long getItemId(int position) {
     return position;
 }

 public View getView(int position, View convertView, ViewGroup parent) {

  //Use this code if you want to load from resources
     ImageView i = new ImageView(mContext);
     i.setImageResource(mImageIds[position]);
     i.setLayoutParams(new CoverFlow.LayoutParams(130, 130));
     i.setScaleType(ImageView.ScaleType.CENTER_INSIDE); 

     //Make sure we set anti-aliasing otherwise we get jaggies
     BitmapDrawable drawable = (BitmapDrawable) i.getDrawable();
     drawable.setAntiAlias(true);
     return i;

  //return mImages[position];
 }
 /** Returns the size (0.0f to 1.0f) of the views 
  * depending on the 'offset' to the center. */ 
  public float getScale(boolean focused, int offset) { 
    /* Formula: 1 / (2 ^ offset) */ 
      return Math.max(0, 1.0f / (float)Math.pow(2, Math.abs(offset))); 
  } 

}
}

解决方案

LayoutInflater inflater = (LayoutInflater) this
 .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View v = inflater.inflate(R.layout."layout here", null, false);

and to use,eg

 TextView tv=(TextView)v.findViewById(R,id."id here");